This page is still under construction.

Parts of this page are still being built. What you see may change.

Towers

Time limit10sMemory limit256 MB

Summary
Fill an n by n Latin square with heights 1 to n that matches fixed cells and border visibility counts, printing the smallest solution or no.
Level

Medium6 of 10

Topics
Backtracking, Brute force
Solved
No attempts yet

Problem

Place one tower in every cell of an n×nn \times n grid. Each tower has an integer height between 11 and nn, and no two towers of the same height may share a row or a column, so every row and every column holds each height from 11 to nn exactly once.

A puzzle adds two kinds of constraints.

First, some cells of the grid have a required height, and the tower placed there must have that height.

Second, some positions around the border carry a number. That number is the exact count of towers you see when you look into the grid along that row or column from that side. A taller tower completely hides every shorter tower behind it, so a tower is visible only when it is taller than every tower between it and the viewer.

Find a placement that satisfies every constraint.

Input

The first line has the number of puzzles TT. (1≤T≤1001 \le T \le 100)

Each puzzle starts with a line holding the grid size nn (3≤n≤53 \le n \le 5), followed by n+2n+2 lines of n+2n+2 characters each.

The first of those lines is the top border and the last one is the bottom border. The first character of each of the middle nn lines is the left border and the last character is the right border. A digit on the border is the number of towers visible from that direction, and '-' means that direction has no constraint. The four corners are always '-'.

The inner nn characters of each of the middle nn lines are the cells of the grid. A digit is the required height of the tower in that cell, and '-' means the height is not fixed.

Every character of a puzzle is either '-' or a digit between 11 and nn.

Output

For each puzzle print the answer as nn lines of nn digits, then print one blank line.

If several placements satisfy the constraints, print the lexicographically smallest one. Compare two answers as the n2n^2 digit string formed by reading the grid top to bottom and each row left to right.

If the puzzle has no solution, print the single word no, then print one blank line.

Examples2

  1. Example 1

    Input
    5
    5
    -------
    -------
    -------
    -------
    -------
    -------
    -------
    5
    -41223-
    2-----3
    3-----2
    2-----1
    1-----5
    3-----2
    -23212-
    3
    -111-
    ----3
    2---2
    ----2
    -131-
    5
    --33---
    ------3
    -------
    ------3
    -------
    3------
    --324--
    5
    -----1-
    -------
    2--3--3
    2-----2
    -1-----
    ----1-2
    -------
    
    Expected output
    12345
    21453
    34512
    45231
    53124
    
    15243
    23514
    42135
    54321
    31452
    
    no
    
    51243
    24351
    45132
    13524
    32415
    
    31425
    25341
    42153
    14532
    53214
    
  2. Example 2

    Input
    1
    3
    -231-
    -----
    -----
    -----
    -----
    
    Expected output
    213
    321
    132