题目
# 填涂颜色
https://www.luogu.com.cn/problem/P1162
## 题目描述
由数字 $0$ 组成的方阵中,有一任意形状闭合圈,闭合圈由数字 $1$ 构成,围圈时只走上下左右 $4$ 个方向。现要求把闭合圈内的所有空间都填写成 $2$。例如:$6\times 6$ 的方阵($n=6$),涂色前和涂色后的方阵如下:
```plain
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
```
```plain
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1
```
## 输入格式
每组测试数据第一行一个整数 $n(1 \le n \le 30)$。
接下来 $n$ 行,由 $0$ 和 $1$ 组成的 $n \times n$ 的方阵。
方阵内只有一个闭合圈,圈内至少有一个 $0$。
//感谢黄小U饮品指出本题数据和数据格式不一样. 已修改(输入格式)
## 输出格式
已经填好数字 $2$ 的完整方阵。
## 样例 #1
### 样例输入 #1
```
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
```
### 样例输出 #1
```
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1
```
## 提示
对于 $100\%$ 的数据,$1 \le n \le 30$。
AC
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n;
int v[35][35];
int dx[4] = {0,0,-1,1}, dy[4] = {1,-1,0,0};
int a[35][35];
struct node {
int x, y;
}t, p;
queue<node> q;
void bfs(int u1,int u2) {
t.x = u1, t.y = u2;
if (v[u1][u2]) return;
v[u1][u2] = 1;
q.push(t);
while (!q.empty()) {
t = q.front(), q.pop();
for (int i = 0; i < 4; i++) {
int cx = t.x + dx[i];
int cy = t.y + dy[i];
if (cx<0||cy<0||cx>=n||cy>=n) continue;
if (!v[cx][cy] && a[cx][cy]==0) {
v[cx][cy] = 1;
p.x = cx, p.y = cy;
q.push(p);
}
}
}
}
int main () {
cin>>n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin>>a[i][j];
}
}
for (int i = 0; i < n; i++) {
if (a[0][i]==0) bfs(0,i);
}
for (int i = 0; i < n; i++) {
if (a[n-1][i]==0) bfs(n-1,i);
}
for (int i = 0; i < n; i++) {
if (a[i][0]==0) bfs(i,0);
}
for (int i = 0; i < n; i++) {
if (a[i][n-1]==0) bfs(i,n-1);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j]==0 && !v[i][j]) cout<<"2";
else cout<<a[i][j];
cout<<" ";
}
cout<<endl;
}
return 0;
}