#include<bits/stdc++.h>
using namespace std;
queue <int> l;
queue <int> r;
int dx[4]= {1,0,-1,0};
int dy[4]= {0,1,0,-1};
int n,m;
int a[31][31];
int gh[31][31];
int bfs(int c,int b) {
gh[c][b]=1;
l.push(c);
r.push(b);
while(!l.empty()) {
for(int i=0; i<4; i++) {
int xx=c+dx[i];
int yy=b+dy[i];
if(gh[xx][yy]!=1&&a[xx][yy]!=1&&xx>0&&xx<=n&&yy>0&&yy<=n) {
gh[xx][yy]=1;
l.push(xx);
r.push(yy);
}
}
l.pop(),r.pop();
}
}
int main() {
cin>>n;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++) {
cin>>a[i][j];
if(a[i][j]==1)
gh[i][j]=1;
}
for(int i=1; i<=n; i++) {
if(!a[i][1]) bfs(i,1);
if(!a[n][i]) bfs(n,i);
if(!a[1][i]) bfs(1,i);
if(!a[i][n]) bfs(i,n);
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if(gh[i][j]!=1) {
cout<<2<<" ";
}
else
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
为什么RE谢谢