Light Up

Time limit2sMemory limit256 MB

Summary
Place bulbs on white cells of an N by N Light Up board so every white cell is lit and each numbered black cell has the required count of adjacent bulbs, choosing the lexicographically smallest solution.
Level

Medium7 of 10

Topics
Backtracking, Brute force, Implementation, Matrix
Solved
No attempts yet

Problem

Light Up is a puzzle played on a square board. Each cell of an N×NN \times N board is either a black square or a white square. The goal is to place light bulbs on some white squares so that every white square is lit.

A white square is lit when a bulb stands in the same row or the same column and no black square lies between them. A square that holds a bulb is lit as well. A bulb can be placed only on a white square.

Figure 1

Figure 1

Placing a bulb at (3,3)(3, 3) on the board of figure 1 gives the situation in figure 2.

Figure 2

Figure 2

Placing a bulb on a white square that is already lit overheats the bulb, so such a placement is not allowed. Bulbs at both (2,3)(2, 3) and (3,3)(3, 3), as in figure 3, are impossible.

Figure 3

Figure 3

Some black squares carry a digit. The digit is the number of squares that must hold a bulb among the squares sharing an edge with that black square. Look at the board in figure 4.

Figure 4

Figure 4

Figure 5 shows one placement that satisfies every rule on the board of figure 4.

Figure 5

Figure 5

Given a board, find a placement that solves the puzzle.

Input

The first line holds the number of test cases TT. (1≤T≤301 \le T \le 30)

The first line of each test case holds the board size NN. (1≤N≤71 \le N \le 7)

Each of the next NN lines holds NN integers separated by a space. The jj-th integer of the ii-th line is the description RijR_{ij} of the cell at (i,j)(i, j). A value of −2-2 means a white square, −1-1 means a black square with no digit, and a value from 00 to 44 means a black square carrying that digit.

Output

For each test case print NN lines, each holding NN integers equal to 00 or 11 and separated by a space. Print 11 for a cell that holds a bulb and 00 for a cell that does not. Print the answers of the test cases in the order the boards are given.

Every input has at least one valid placement. When more than one placement satisfies the rules, print only the lexicographically smallest one. Read a placement from the first line to the last, and inside a line from left to right, as a sequence of N×NN \times N digits 00 and 11. The answer is the placement whose sequence is smallest in lexicographic order. In other words, walking the cells in that order, a cell holds 00 whenever the puzzle can still be finished with 00 in it.

Examples5

  1. Example 1

    Input
    2
    7
    -2 -2 -2 -2 -2 0 -2
    1 -2 -2 -2 -2 -2 -2
    -2 -2 1 -2 2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 0 -2 2 -2 -2
    -2 -2 -2 -2 -2 -2 2
    -2 1 -2 -2 -2 -2 -2
    7
    -2 -2 -1 -2 -2 -1 -2
    3 -2 -2 -2 -2 -2 -2
    -2 -2 -2 2 -2 -2 -1
    -2 -2 -1 -2 3 -2 -2
    -1 -2 -2 3 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -1
    -2 2 -2 -2 0 -2 -2
    
    Expected output
    1 0 0 0 0 0 0
    0 0 0 1 0 0 0
    0 1 0 0 0 1 0
    0 0 0 0 1 0 0
    0 0 0 0 0 0 1
    0 0 0 0 1 0 0
    1 0 0 0 0 0 1
    1 0 0 1 0 0 1
    0 1 0 0 0 0 0
    1 0 0 0 1 0 0
    0 0 0 1 0 0 1
    0 0 0 0 1 0 0
    0 0 0 1 0 0 0
    1 0 1 0 0 0 1
    
  2. Example 2

    Input
    3
    1
    -2
    1
    -1
    1
    0
    
    Expected output
    1
    0
    0
    
  3. Example 3

    Input
    1
    7
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    
    Expected output
    0 0 0 0 0 0 1
    0 0 0 0 0 1 0
    0 0 0 0 1 0 0
    0 0 0 1 0 0 0
    0 0 1 0 0 0 0
    0 1 0 0 0 0 0
    1 0 0 0 0 0 0
    
  4. Example 4

    Input
    7
    1
    -2
    2
    -2 -2
    -2 -2
    3
    -2 -2 -2
    -2 -2 -2
    -2 -2 -2
    4
    -2 -2 -2 -2
    -2 -2 -2 -2
    -2 -2 -2 -2
    -2 -2 -2 -2
    5
    -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2
    6
    -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2
    7
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    -2 -2 -2 -2 -2 -2 -2
    
    Expected output
    1
    0 1
    1 0
    0 0 1
    0 1 0
    1 0 0
    0 0 0 1
    0 0 1 0
    0 1 0 0
    1 0 0 0
    0 0 0 0 1
    0 0 0 1 0
    0 0 1 0 0
    0 1 0 0 0
    1 0 0 0 0
    0 0 0 0 0 1
    0 0 0 0 1 0
    0 0 0 1 0 0
    0 0 1 0 0 0
    0 1 0 0 0 0
    1 0 0 0 0 0
    0 0 0 0 0 0 1
    0 0 0 0 0 1 0
    0 0 0 0 1 0 0
    0 0 0 1 0 0 0
    0 0 1 0 0 0 0
    0 1 0 0 0 0 0
    1 0 0 0 0 0 0
    
  5. Example 5

    Input
    1
    7
    -2 -2 -2 -2 -2 -2 -2
    -1 -1 -1 -1 -1 -1 -1
    -2 -2 -2 -2 -2 -2 -2
    -1 -1 -1 -1 -1 -1 -1
    -2 -2 -2 -2 -2 -2 -2
    -1 -1 -1 -1 -1 -1 -1
    -2 -2 -2 -2 -2 -2 -2
    
    Expected output
    0 0 0 0 0 0 1
    0 0 0 0 0 0 0
    0 0 0 0 0 0 1
    0 0 0 0 0 0 0
    0 0 0 0 0 0 1
    0 0 0 0 0 0 0
    0 0 0 0 0 0 1