一直挂第四个点,请大佬指正错误
我也不知道为什么非要写BFS折磨自己qwq
#include<iostream>
#include<queue>
using namespace std;
int n, map[35][35];
bool vis[35][35];
int to[5][2] = {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
struct node {
int x, y;
};
queue<node> q;
inline void bfs(int a, int b) {
node t = {a, b};
q.push(t);
vis[a][b] = 1;
while(!q.empty()) {
node k = q.front();
int xx = k.x, yy = k.y;
q.pop();
for(int i = 1; i <= 4; i++) {
int kx = xx + to[i][0], ky = yy + to[i][1];
if(!vis[kx][ky] and kx > 0 and kx <= n and ky > 0 and ky <= n) {
vis[kx][ky] = 1;
node tmp = {kx, ky};
q.push(tmp);
}
}
}
}
int main() {
cin>>n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin>>map[i][j];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(map[i][j] == 1)
vis[i][j] = 1;
for(int i = 1; i <= n; i++)
if(map[i][1] == 0 and !vis[i][1])
bfs(i, 1);
for(int i = 1; i <= n; i++)
if(map[i][n] == 0 and !vis[i][n])
bfs(i, n);
for(int i = 1; i <= n; i++)
if(map[1][i] == 0 and !vis[1][i])
bfs(1, i);
for(int i = 1; i <= n; i++)
if(map[1][n] == 0 and !vis[1][n])
bfs(n, i);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
if(!vis[i][j] and map[i][j] == 0)
map[i][j] = 2;
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++)
cout<<map[i][j]<<" ";
cout<<'\n';
}
return 0;
}