leo888 @ 2021-07-28 11:58:05
错误出现在bfs里面,这个rear指针遍历不下去,导致vis数组判断不了,所以求大佬帮助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include<bits/stdc++.h>
using namespace std;
int n,head,rear,i, j;
const int fx[4]={1,0,-1,0};
const int fy[4]={0,1,0,-1};
int a[10001][10001];
bool vis[1001][1001];
struct node{
int x1;
int y1;
int step;
}q[1001];
int bfs(int x,int y){
head = 0, rear = 0;
q[rear].x1=x;
q[rear].y1=y;
q[rear].step=0;
rear++;
vis[x][y]=1;
while(head<rear){
node now=q[head];
head++;
for(int k=0;k<4;k++){
int tx=now.x1+fx[k];
int ty=now.y1+fy[k];
if(tx>=0 && tx<n && ty>=0 && ty<n && vis[tx][ty]==0 && a[tx][ty]==0){
cout << tx << ' ' << ty << endl;
vis[tx][ty]=1;
q[rear].x1=tx;
q[rear].y1=ty;
rear++;
}
}
}
}
int main(){
int n;
cin>>n;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cin>>a[i][j];
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(a[i][j]==0 && (i==0 || j==0 || i==n-1 || j==n-1)){
bfs(i,j);
}else continue;
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(vis[i][j]==1 || a[i][j]==1)cout<<a[i][j]<<' ';
else cout<<2<<' ';
}
cout<<endl;
}
return 0;
}
by 于洪铎 @ 2021-07-28 13:27:49
建议:重构rear,用另一个变量检测rear。
by 庄nnnn额 @ 2021-08-02 11:07:57
建议用STL队列。
by chenyitian @ 2021-08-08 23:00:46
用DFS吧,DFS更快