#include<bits/stdc++.h>
using namespace std;
int n,a[35][35],dx[]={0,1,0,-1},dy[]={1,0,-1,0};
struct node{
int xx,yy;
}st,nx;
queue<node>q;
void bfs(int x,int y){
a[x][y]=2;
q.push({x,y});
while(!q.empty()){
st=q.front();
q.pop();
for(int i=0;i<4;++i){
nx.xx=st.xx+dx[i];
nx.yy=st.yy+dy[i];
if(nx.xx>=1 && nx.xx<=n && nx.yy>=1 &&
nx.yy<=n && a[nx.xx][nx.yy]==0){
a[nx.xx][nx.yy]=2;
q.push(nx);
}
}
}
}
int main(){
int i,j,f=0;
cin>>n;
for(i=1;i<=n;++i){
for(j=1;j<=n;++j) cin>>a[i][j];
}
for(int i=1;i<=n;++i){
for(j=1;j<=n;++j){
if(a[i][j]==0){
bfs(i,j);
break;
}
}
if(f) break;
}
for(int i=1;i<=n;++i){
for(j=1;j<=n;++j){
cout<<2-a[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
下列输入怎么办:
3
1 1 1
1 0 1
1 1 1
1 1 1
1 2 1
1 1 1