#P1162. 填涂颜色

    ID: 162 远端评测题 1000ms 125MiB 尝试: 1 已通过: 0 难度: 3 上传者: 标签>搜索洛谷原创广度优先搜索,BFS队列

填涂颜色

Description

In a square grid consisting of digits 00, there is a closed loop formed by digits 11 of arbitrary shape. The task is to fill all spaces inside the closed loop with 22. For example: for a 6×66 \times 6 grid (n=6n = 6), the grids before and after filling are as follows:

If starting from some 00, moving only in the 44 directions up, down, left, and right, and passing only through other 00s, it is impossible to reach the boundary of the grid, then this 00 is considered to be inside the closed loop. The closed loop is not necessarily ring-shaped; it can be any shape, but it is guaranteed that the 00s inside the closed loop are connected (any two can reach each other).

0 0 0 0 0 0
0 0 0 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 1 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 0 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 1 2 1
1 1 1 1 1 1

Input Format

For each test case, the first line contains an integer nn (1n301 \le n \le 30).

The next nn lines contain an n×nn \times n grid consisting of digits 00 and 11.

There is exactly one closed loop in the grid, and there is at least one 00 inside the loop.

Output Format

Output the complete grid after filling the inside of the closed loop with the digit 22.

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

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

Hint

For 100%100\% of the testdata, 1n301 \le n \le 30.

Translated by ChatGPT 5