题解
2024-08-20 09:39:30
发布于:广东
4阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int N = 50;
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
struct node{
int x, y, step;
};
int r, c;
char a[N][N];
bool vis[N][N] = {0};
int bfs(int x, int y){
vis[x][y] = true;
queue<node> q;
q.push({x, y, 1});
while(q.size()){
node now = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int nx = now.x + dir[i][0];
int ny = now.y + dir[i][1];
int step = now.step + 1;
if(nx >= 0 && nx < r && ny >= 0 && ny < c && !vis[nx][ny] && a[nx][ny] == '.'){
vis[nx][ny] = true;
q.push({nx, ny, step});
}
if(nx == r - 1 && ny == c - 1){
return step;
}
}
}
}
int main(){
cin >> r >> c;
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
cin >> a[i][j];
}
}
cout << bfs(0, 0);
return 0;
}
这里空空如也
有帮助,赞一个