当前位置:网站首页>(bfs) acwing 844. Labyrinth
(bfs) acwing 844. Labyrinth
2022-06-13 09:24:00 【Age worry】
844. Labyrinth
Topic link https://www.acwing.com/problem/content/846/
subject :
Ideas :bfs
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef struct Node{
int x,y,ct;
}node;
int n,m;
bool vis[110][110];
int a[110][110];
int fx[4][2]={
{
1,0},{
-1,0},{
0,1},{
0,-1}};
int res=10010;
node q[10010];
int bfs(){
int f=0,r=-1;
q[++r]={
1,1,0};
while(f<=r){
node temp=q[f++];
if(temp.x==n&&temp.y==m){
return temp.ct;
break;
}
for(int i=0;i<4;i++){
int xx=temp.x+fx[i][0],yy=temp.y+fx[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!a[xx][yy]&&!vis[xx][yy]){
q[++r]={
xx,yy,temp.ct+1};
vis[xx][yy]=1;
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
cout<<bfs();
return 0;
}
dfs+ prune , But I can't pass
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
bool vis[110][110];
int a[110][110];
int fx[4][2]={
{
1,0},{
-1,0},{
0,1},{
0,-1}};
int res=10010;
void dfs(int x,int y,int ct){
vis[x][y]=1;
if(x==n&&y==m){
res=min(res,ct);
vis[x][y]=0;
return ;
}
if(res<ct){
vis[x][y]=0;
return ;
}
for(int i=0;i<4;i++){
int xx=x+fx[i][0],yy=y+fx[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!a[xx][yy]&&!vis[xx][yy]){
dfs(xx,yy,ct+1);
}
}
vis[x][y]=0;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
dfs(1,1,0);
cout<<res;
return 0;
}
边栏推荐
- Attack and defense world PWN shell
- Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
- C language: deep understanding of pointers and arrays
- 20211104 why are the traces of similar matrices the same
- C language: deep understanding of character functions and string functions (1)
- LeetCode 72. 编辑距离
- C language: file operation
- C language: callback function
- LeetCode 6097. 替换字符后匹配(字典)
- 【最全面详细解释】背包问题详解
猜你喜欢

Final principle

Exploitation of competitive loopholes in attacking and defending world PWN play conditions

阿里高级专家剖析 | Protocol的标准设计

Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5

IP address introduction

an error occurred while trying to rename a file in the destination directory code 5

JUC 原子累加器 源码之 LongAdder

Jenkins接入Openldap用户认证

20211104 why are the traces of similar matrices the same

Jenkins accédant à l'authentification de l'utilisateur openldap
随机推荐
Jenkins集成Ldap,Ldap配置错误导致jenkins用户登录失败问题解决
ROS2之OpenCV人脸识别foxy~galactic~humble
Haproxy + keepalived for high availability load balancing of MySQL
Simple implementation of database link pool
Collection of articles on virtualization and cloud computing
Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
Neo4j環境搭建
How Simulink adds modules to the library browser
Dpdk timer learning notes
攻防世界PWN play 条件竞争漏洞的利用
攻防世界-PWN-shell
CAS NO lock
阿里高级专家剖析 | Protocol的标准设计
C language: sanziqi
turtle库的使用数字时钟模拟时钟动态显示
JUC atomic integer
JUC Unsafe
时间戳转localDate
20211006 linear transformation
JUC atomic array