https://www.luogu.org/problem/show?pid=1162

//其实很简单的吧
//就是最外圈加一圈0 ,然后把外圈里面的0都遍历了
//剩下的0 就变成2 就行了
#include<bits/stdc++.h>
using namespace std;
typedef pair <int,int> pii;
int n ,s[][];
bool vis[][];
int fx[]={-,,,};
int fy[]={,-,,}; bool check(int x,int y)
{
if(!vis[x][y] && s[x][y] == && x>= && x<=n+ && y>= && y<=n+)
return ;
return ;
} void bfs(int x,int y)
{
queue<pii > que;
que.push({x,y});
//cout<<"yes1"<<endl;
while (que.size() )
{
pii now = que.front(); que.pop();
if( vis[now.first][now.second] )
continue;
vis[now.first][now.second] = ;//注意该点已经访问过 这里很容易忘记的吧
//cout<<"yes2"<<endl;
for(int i=;i<;i++)
{
//cout<<"yes3"<<endl;
int dx= now.first+fx[i];
int dy= now.second+fy[i]; if(check(dx,dy))
{
//cout<<"yes3"<<endl;
que.push({dx,dy});
}
}
}
} int main ()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&s[i][j]);
//cout<<endl;
//因为外圈已经本身就是0了
//从第0行到第n+1行
bfs(,); for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if( vis[i][j] )
cout<<<<" ";
else if(s[i][j] == )
cout<<<<" ";
else
cout<<<<" ";
}
cout<<endl;
}
}
05-04 01:22