题解
2024-08-19 12:06:06
发布于:广东
3阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
const int N=45;
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
char lo[N][N];
int vis[N][N];
int n,m;
struct node{
int x,y,step;
}r,l;
void bfs(int x,int y,int step){
queue<node> q;
q.push({x,y,step});
vis[x][y]=1;
while(q.size()){
r=q.front();
q.pop();
if(r.x==n && r.y==m){
cout<<r.step;
break;
}
for(int i=0;i<4;i++){
l.x=r.x+dx[i];
l.y=r.y+dy[i];
l.step=r.step+1;
if(l.x>=1&&l.x<=n&&l.y>=1&&l.y<=m&&lo[l.x][l.y]!='#'&&vis[l.x][l.y]==0){
vis[l.x][l.y]=1;
q.push(l);
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>lo[i][j];
}
}
bfs(1,1,1);
return 0;
}
这里空空如也
有帮助,赞一个