题解
2025-09-06 17:55:27
发布于:广东
0阅读
0回复
0点赞
#include<iostream>
#include<queue>
using namespace std;
char mp[50][50];
int n,m;
bool vis[50][50];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
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();
if(r.x == n && r.y == m){
cout<<r.step;
return;
}
q.pop();
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 && mp[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>>mp[i][j];
}
}
bfs(1,1,1);
return 0;
}
//Tian
这里空空如也


有帮助,赞一个