迷宫判定
2025-01-23 10:03:45
发布于:上海
#include<bits/stdc++.h>
using namespace std;
int n,m;
int g[41][41];
int dist[41][41];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node{
int x,y;
};
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>g[i][j];
}
}
queue<node> q;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dist[i][j] = 0x3f3f3f3f;
}
}
dist[1][1] = 0;
q.push({1,1});
while(q.size()){
node top = q.front();
q.pop();
for(int i=0;i<4;i++){
int nx = top.x+dir[i][0];
int ny = top.y+dir[i][1];
if(nx<1 || ny<1 || nx>n || ny>m || g[nx][ny]==1)continue;
if(dist[nx][ny]>dist[top.x][top.y]+1){
dist[nx][ny]=dist[top.x][top.y]+1;
q.push({nx,ny});
}
}
}
if(dist[n][m]==0x3f3f3f3f)cout<<"NO";
else cout<<"YES";
return 0;
}
这里空空如也
有帮助,赞一个