```cpp
//拙劣的处理
//噢对了之前我写的移动方向有个问题就是不能八个
//方向移动只能四个方向移动否则会wa第二个点
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 166;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int map[maxn][maxn];
bool vis[maxn][maxn];
int n,m,total;
queue<int> qx,qy;
void bfs(int x,int y){
qx.push(x);qy.push(y);
while(!qx.empty()){
int vx=qx.front();qx.pop();
int vy=qy.front();qy.pop();
if(map[vx][vy]==1)
break;
vis[vx][vy]=true;
for(int i=0;i<4;i++){
int nx=vx+dx[i];
int ny=vy+dy[i];
if(map[nx][ny]==0&&!vis[nx][ny]&&nx<n&&ny<n&&nx>=0&&ny>=0){
vis[nx][ny]=true;
qy.push(ny);qx.push(nx);
}
}
}
}
void solve(){
for(int i=0;i<n;i++){
bfs(0,i);
bfs(n-1,i);
bfs(i,0);
bfs(i,n-1);
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
cin>>map[i][j];
}
solve();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==0||i==n-1||j==0||j==n-1){
cout<<map[i][j]<<" ";
}
else if(map[i][j]==0&&!vis[i][j])
cout<<2<<" ";
else cout<<map[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
```
by Umaru @ 2017-09-02 13:25:20