eolymp
bolt
Try our new interface for solving problems
Problems

Nurikabe

Nurikabe

Your goal is to write a solver for Nurikabe, a binary determination puzzle. The puzzle is played on a grid, typically rectangular (with no standard size) containing empty and numbered cells. You must decide for each cell if it is white (land) or black (water), so that it satisfies the following constraints. An Island is a maximal connected region of white cells. \includegraphics{https://static.e-olymp.com/content/c6/c6126ae08fa3b1985ebb14a43897728235101a36.jpg} \begin{itemize} \item The water areas must form one connected region. (All the black cells must be connected.) \item Each numbered cell must be part of an island. \item The number of cells in an island is equal to the number it contains. \item Every region (island) of white cells (land) must contain exactly one number. \item Two islands may not be connected. \item \textbf{2}×\textbf{2} blocks of black squares are not allowed. \end{itemize} Note that diagonal adjacency doesn't count as connectedness. You can assume there is always a unique solution for each puzzle. \InputFile There are multiple test cases in the input. The first line of each test case contains two numbers \textbf{n}, \textbf{m} (\textbf{3} ≤ \textbf{n}, \textbf{m} ≤ \textbf{9}) which are the dimensions of the puzzle, followed by \textbf{n} lines each one has \textbf{m} characters including '\textbf{.}' (indicating an empty cell) and \textbf{1}-digit numbers. The last line of the input contains two zero numbers. \OutputFile The output for each test case should show the solved puzzle. Show black (water) cells with '\textbf{#}'. Write an empty line in the output after each puzzle.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3 4
3...
....
.4..
5 5
2.5..
.....
.....
.....
..4.3
0 0
Output example #1
3..#
####
.4..

2#5..
.#.##
##.#.
.###.
..4#3
Source 32nd ACM International Collegiate, 9th Asian Regional Contest in Iran, December 6-7 2007 (Azar 15-16, 1386)