It's been a long day
2024-09-08 12:51:14
发布于:广东
1阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int a[10][10];
bool vis[10][10];
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct node{
int x, y, step;
};
int bfs(int x, int y){
queue<node> q;
q.push({x, y, 0});
vis[0][0] = 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;
if(nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && !vis[nx][ny] && !a[nx][ny]){
q.push({nx, ny, step + 1});
vis[nx][ny] = 1;
}
if(nx == 4 && ny == 4){
return step + 1;
}
}
}
return -1;
}
int main(){
memset(vis, 0, sizeof(vis));
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
cin >> a[i][j];
}
}
cout << bfs(0, 0);
return 0;
}
这里空空如也
有帮助,赞一个