样例能过
by zhenghuangzhe @ 2024-08-09 12:01:24
@[zhenghuangzhe](/user/737350)
看看吧
```
/*
1、P1162 填涂颜色
*/
#include<bits/stdc++.h>
using namespace std;
int a[40][40],n,b[40][40];
int xp[5]={0,1,0,-1,0};
int yp[5]={0,0,1,0,-1};
void dfs(int x,int y){
if(x<0 || x>n+1 || y<0 || y>n+1 || b[x][y]!=0)return ;
b[x][y]=1;
for(int i=1;i<=4;i++)dfs(x+xp[i],y+yp[i]);
}
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]==0)b[i][j]=0;
else b[i][j]=2;
}
}
dfs(0,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(b[i][j]==0)cout<<2<<" ";
else cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
```
by haimingbei @ 2024-08-09 12:08:59
@[zhenghuangzhe](/user/737350)
输出想复杂了
如果used[i][j]没被标记过,输出2
否则,输出a[i][j];
在这之前的在输入时加上
```cpp
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
if (a[i][j]==0){
used[i][j]=0;
}else{
used[i][j]=2;
}
```cpp
```
望观(逃
by akksc04 @ 2024-08-09 22:06:24