#include <bits/stdc++.h>
using namespace std;
char mp[35][55];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int vis[35][55] = {0};
struct node{
int x, y, step;
bool friend operator < (node a,node b){
return a.step > b.step;
}
};
priority_queue<node>que;
int bfs(){
node st = {0, 0, 0};
que.push(st);
while(que.size()){
node cur = que.top();
cout<<cur.step<<endl;
if(cur.x == 29 && cur.y == 49) return cur.step;
que.pop();
for(int i = 0; i < 4; i++){
int xx = cur.x + dx[i];
int yy = cur.y + dy[i];
if(xx < 0 || xx >= 30 || yy < 0 || yy >= 50 || mp[xx][yy] == '1' || vis[xx][yy]) continue;
vis[xx][yy] = 1;
node tmp = {xx, yy, cur.step + 1};
que.push(tmp);
}
}
return -1;
}
int main(){
for(int i = 0; i < 30; i++){
for(int j = 0; j < 50; j++){
cin>>mp[i][j];
}
}
cout<<bfs()<<endl;
return 0;
}
bfs模板
最新推荐文章于 2025-05-02 15:12:23 发布