454. Kakuro

Time limit per test: 2.75 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard

Kakuro puzzle is played on a nx m grid of "black" and "white" cells. Apart from the top row and leftmost column which are entirely black, the grid has some amount of white cells which form "runs" and some amount of black cells. "Run" is a vertical or horizontal maximal one-lined block of adjacent white cells. Each row and column of the puzzle can contain more than one "run". Every white cell belongs to exactly two runs — one horizontal and one vertical run. Each horizontal "run" always has a number in the black half-cell to its immediate left, and each vertical "run" always has a number in the black half-cell immediately above it. These numbers are located in "black" cells and are called "clues".

The rules of the puzzle are simple:

Given the grid, your task is to find a solution for the puzzle.

Picture of the first sample input

Picture of the first sample output

The first line of input contains two integers n and m (2 ≤ n,m ≤ 6) — the number of rows and columns correspondingly. Each of the next n lines contains descriptions of m cells. Each cell description is one of the following 5-character strings: The first row and the first column of the grid will never have any white cells. The given grid will have at least one "white" cell.

It is guaranteed that the given puzzle has at least one solution.

Print n lines to the output with m cells in each line. For every "black" cell print '' (underscore), for every "white" cell print the corresponding digit from the solution. Delimit cells with a single space, so that each row consists of 2m-1 characters.

If there are many solutions, you may output any of them.

sample input
sample output
6 6
XXXXX 22\22 ..... ..... ..... 10\XX
XX\34 ..... ..... ..... ..... .....
XX\14 ..... ..... 16\13 ..... .....
XX\22 ..... ..... ..... ..... XXXXX
XXXXX XX\16 ..... ..... XXXXX XXXXX
_ _ _ _ _ _
_ _ 5 8 9 _
_ 7 6 9 8 4
_ 6 8 _ 7 6
_ 9 2 7 4 _
_ _ 7 9 _ _

sample input
sample output
3 3
XX\04 ..... XXXXX
_ _ _
_ 4 _
_ _ _

Online Contester Team © 2002 - 2010. All rights reserved.