This page is still under construction.

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

Number Square

Time limit1sMemory limit1024 MB

Summary
Fill an N x N Latin square with 1..N given some pre-filled cells and inequalities between neighboring cells, choosing the lexicographically smallest valid board.
Level

Hard8 of 10

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

Problem

In Number Square you place the numbers 1,2,…,N1, 2, \ldots, N into an N×NN \times N grid so that every number appears exactly once in each row and exactly once in each column (the grid is a Latin square).

In addition, for some pairs of neighbouring cells you are told which of the two must hold the larger number. Given the pre-filled cells and these ordering constraints, fill in the whole grid so that all of them are satisfied.

Input

The first line contains a single integer NN (1≤N<101 \le N < 10).

The next 2N−12N - 1 lines each contain exactly 2N−12N - 1 characters and describe the starting position. Using 0-based indices, cell (r,c)(r, c) of the board is located at line 2r2r, column 2c2c.

  • On the even-indexed lines (0,2,4,…0, 2, 4, \ldots): the even-indexed columns hold the board cells — a digit 1–9 for a pre-filled cell, or . if it is empty. The odd-indexed columns hold the horizontal ordering signs between two side-by-side cells: <, >, or . when there is no constraint.
  • On the odd-indexed lines (1,3,…1, 3, \ldots): the even-indexed columns hold the vertical ordering signs between two stacked cells: ^, V, or . when there is no constraint. The odd-indexed columns are always ..

Each sign is an inequality that opens toward the larger number: < means the left cell is smaller than the right one, > means the left cell is larger, ^ means the upper cell is smaller than the lower one, and V means the upper cell is larger.

Output

It is guaranteed that at least one valid filling exists. Print the completed board as NN lines, each containing NN integers separated by single spaces.

If more than one valid filling exists, print the lexicographically smallest one, where a completed board is compared as the sequence obtained by reading its cells row by row from top to bottom and, within each row, from left to right.

Examples3

  1. Example 1

    Input
    3
    1....
    ..^.V
    .<...
    .....
    .>..2
    
    Expected output
    1 2 3
    2 3 1
    3 1 2
    
  2. Example 2

    Input
    1
    .
    
    Expected output
    1
    
  3. Example 3

    Input
    2
    .<.
    ^.V
    .>.
    
    Expected output
    1 2
    2 1