Problem C
Flood-It
Flood-It is a popular one player game on many smart phones.
The player is given an
A player makes a move by choosing one of the
It has been proven that finding the optimal moves is a very hard problem. For this problem, you will simulate a very simple greedy strategy to see how well it works:
-
for each move, choose the colour that will result in the largest number of tiles connected to the origin;
-
if there is a tie, break ties by choosing the lowest numbered colour.
To illustrate this, we look at the first test case in the sample input, the original board is:
![\includegraphics[width=0.2\textwidth ]{fig1.pdf}](/problems/floodit/file/statement/en/img-0001.png)
If we choose colour
![\includegraphics[width=0.2\textwidth ]{fig2.pdf}](/problems/floodit/file/statement/en/img-0002.png)
where the tiles connected to the origin are shaded. In the
next move, we choose colour
![\includegraphics[width=0.2\textwidth ]{fig3.pdf}](/problems/floodit/file/statement/en/img-0003.png)
Input
The input consists of multiple test cases. The first line of
input is a single integer, not more than
Output
For each case, display two lines of output. The first line
specifies the number of moves needed to change all the tiles to
the same colour. The second line specifies
Sample Input 1 | Sample Output 1 |
---|---|
4 6 123423 334521 433123 543621 324343 234156 5 12121 21212 12121 21212 12121 5 12345 12345 12345 12345 12345 5 11131 12211 31311 21111 11111 |
12 2 2 4 2 1 1 8 4 4 0 0 0 0 4 0 1 1 1 1 0 4 1 2 1 0 0 0 |