Su-domino-ku

Time limit2sMemory limit128 MB

Summary
Complete a 9x9 Sudoku grid in which 36 dominoes cover the empty cells and every distinct digit pair appears as exactly one domino.
Level

Hard8 of 10

Topics
Backtracking, DFS, Implementation, Hash map
Solved
No attempts yet

Problem

After Sudoku became popular worldwide, many similar puzzles appeared. One of them, Su-domino-ku, combines Sudoku with dominoes.

This puzzle follows the rules of Sudoku. You must fill a 9×99 \times 9 grid with the digits 11 through 99 so that all of the following hold:

  • Each row contains each digit from 11 to 99 exactly once.
  • Each column contains each digit from 11 to 99 exactly once.
  • Each of the nine 3×33 \times 3 squares that partition the grid also contains each digit from 11 to 99 exactly once.

In a Su-domino-ku grid, the digits 11 through 99 are already written, one per cell (nine cells in total), and the remaining 7272 cells must be covered by 3636 domino tiles. Each domino tile holds two different digits, and there is exactly one tile for every unordered pair of distinct digits from 11 to 99 (for example 1+2,1+3,…,1+9,2+3,…1+2, 1+3, \dots, 1+9, 2+3, \dots). Since 1+21+2 and 2+12+1 are the same tile, they are not distinguished. A domino may be placed horizontally or vertically, and it may straddle the border between two 3×33 \times 3 squares.

Cell positions are written as follows. Rows are labeled AA to II from top to bottom, columns are labeled 11 to 99 from left to right, and a cell position is written as (row letter)(column digit). For example, B3B3 denotes row BB, column 33.

Given the initial state of a Su-domino-ku puzzle, write a program that completes it.

Input

The input consists of several test cases.

The first line of each test case contains the number NN of dominoes already placed (10≤N≤3510 \le N \le 35).

Each of the next NN lines describes one domino in the format U LU V LV. UU is one digit written on the domino and LULU is the position of the cell holding it (a string of length 22); VV is the other digit on the domino and LVLV is its position. The two cells of a domino are always horizontally or vertically adjacent.

The following line gives the positions of the nine already-written digits, in order for 11 through 99. Positions use the same notation as the dominoes.

No cell is occupied by more than one domino or digit.

The last line of the input contains a single 00, marking the end of the input.

Output

For each puzzle, first print Puzzle k, where kk is its position in the input (starting from 11). Then print the completed 9×99 \times 9 grid as nine lines of nine digits each.

Only inputs with a unique solution are given.

Examples1

  1. Example 1

    Input
    10
    6 B2 1 B3
    2 C4 9 C3
    6 D3 8 E3
    7 E1 4 F1
    8 B7 4 B8
    3 F5 2 F6
    7 F7 6 F8
    5 G4 9 G5
    7 I8 8 I9
    7 C9 2 B9
    C5 A3 D9 I4 A9 E5 A2 C6 I1
    11
    5 I9 2 H9
    6 A5 7 A6
    4 B8 6 C8
    3 B5 8 B4
    3 C3 2 D3
    9 D2 8 E2
    3 G2 5 H2
    1 A2 8 A1
    1 H8 3 I8
    8 I3 7 I4
    4 I6 9 I7
    I5 E6 D1 F2 B3 G9 H7 C9 E5
    0
    
    Expected output
    Puzzle 1
    872643195
    361975842
    549218637
    126754983
    738169254
    495832761
    284597316
    657381429
    913426578
    Puzzle 2
    814267593
    965831247
    273945168
    392176854
    586492371
    741358629
    137529486
    459683712
    628714935