#P4487. [BJWC2018] Cross sum

[BJWC2018] Cross sum

Description

Game rules:

  • Fill each empty cell with a positive integer.
  • In a cell split by a diagonal line, the number in the upper-right corner equals the XOR sum of the numbers in the consecutive adjacent cells to its right, and the number in the lower-left corner equals the XOR sum of the numbers in the consecutive adjacent cells below it.
  • All integers filled in empty cells must be pairwise distinct (no duplicates).

Apia gave Rimbaud a Kakuro puzzle. Rimbaud is not good at such puzzles at all, so she asks you to help solve it. Since Rimbaud is very young, she can only operate on numbers not exceeding 26012^{60}-1. Therefore, it is required that in the solution of the puzzle, every number does not exceed 26012^{60}-1.

Input Format

Each input contains multiple test cases. The first line contains an integer TT, the number of test cases.

For each test case, the first line contains two positive integers n,mn, m, representing the number of rows and columns of the board.

In the next nn lines, each line contains mm digits from 00 to 44. The digit in row ii, column jj indicates the type of the cell at (i,j)(i, j):

  • 00 means this cell is neither an empty cell nor a clue cell.
  • 11 means this cell contains a clue in the lower-left corner, and no clue in the upper-right corner.
  • 22 means this cell contains a clue in the upper-right corner, and no clue in the lower-left corner.
  • 33 means this cell contains clues in both the lower-left and upper-right corners.
  • 44 means this cell is an empty cell.

The input guarantees that, in terms of format, this is a valid Kakuro puzzle: for every consecutive segment of empty cells, the cell to its left or the cell above it contains a clue.

Then there are nn more lines. Each line contains several non-negative integers, giving all clues in the puzzle in left-to-right order. In particular, if a cell type is 33, then the lower-left clue is given first, followed by the upper-right clue.

Output Format

For each test case, if there is a solution, output nn lines. Each line should output, from left to right, the numbers filled into the empty cells in that row. The filled numbers must be between 11 and 26012^{60}-1 (inclusive). Otherwise, output 1-1.

3
3 3
0 1 1
2 4 4
2 4 4
3 7
2
6
3 3
0 1 1
2 4 4
2 4 4
1 1
1
1
2 2
0 1
2 4
0
0

1 3
2 4
-1
-1

Hint

For 10%10\% of the testdata, it is guaranteed that n,m3n, m \leq 3.

For 30%30\% of the testdata, it is guaranteed that n,m15n, m \leq 15.

For 50%50\% of the testdata, it is guaranteed that n,m40n, m \leq 40.

For another 20%20\% of the testdata, it is guaranteed that only the cell at the first row and first column contains clues, and all remaining cells are empty cells.

For 100%100\% of the testdata, it is guaranteed that 3n,m2003 \leq n, m \leq 200, 1T51 \leq T \leq 5, and every initial clue number does not exceed 26012^{60}-1.

Translated by ChatGPT 5