题解:P1162 填涂颜色
solution
搜索入门。
直接找圈里的
- 处理掉无意义的位置,若当前位置出界或标记过了,就返回。
- 打上标记。
- 搜该位置的上下左右四个位置。
code
#include<bits/stdc++.h>
using namespace std;
int a[40][40], b[40][40], n;
void dfs(int x, int y){
if(x<0||x>n+1||y<0||y>n+1||a[x][y])
return;
a[x][y]=1;
dfs(x, y-1);
dfs(x, y+1);
dfs(x-1, y);
dfs(x+1, y);
}
int main(){
cin >> n;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
cin >> a[i][j],
b[i][j]=a[i][j];
dfs(0, 0);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++)
cout << (a[i][j]?b[i][j]:2) << ' ';
cout << '\n';
}
return 0;
}