哦哦 现在知道了```cpp
#include<iostream>
using namespace std;
int a[100][100];
int search(int k,int g);
int n;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
for(int j=1;j<=n;j++)
{
if(a[1][j]==0)
{
a[1][j]=3;
search(1,j);
}
if(a[n][j]==0)
{
a[n][j]=3;
search(n,j);
}
if(a[j][1]==0)
{
a[j][1]=3;
search(j,1);
}
if(a[j][n]==0)
{
a[j][n]=3;
search(j,n);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]==3)
cout<<0<<" ";
else if(a[i][j]==1)
cout<<1<<" ";
else if(a[i][j]==0)
cout<<2<<" ";
}
cout<<endl;
}
}
int search(int k,int g)
{
int h=0;
a[k][g]=3;
if(k-1>=1&&a[k-1][g]==0)
{
search(k-1,g);
h=1;
}
if(k+1<=n&&a[k+1][g]==0)
{
search(k+1,g);
h=1;
}
if(g-1>=1&&a[k][g-1]==0)
{
search(k,g-1);
h=1;
}
if(g+1<=n&&a[k][g+1]==0)
{
search(k,g+1);
h=1;
}
if(h==0)
return 0;
return 0;
}
```
by 凌霜夜雨 @ 2018-08-22 08:25:39
k+1和g+1应该是<=n;
by 凌霜夜雨 @ 2018-08-22 08:26:25