Su-domino-ku
Time limit2sMemory limit128 MB
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 grid with the digits through so that all of the following hold:
- Each row contains each digit from to exactly once.
- Each column contains each digit from to exactly once.
- Each of the nine squares that partition the grid also contains each digit from to exactly once.
In a Su-domino-ku grid, the digits through are already written, one per cell (nine cells in total), and the remaining cells must be covered by domino tiles. Each domino tile holds two different digits, and there is exactly one tile for every unordered pair of distinct digits from to (for example ). Since and are the same tile, they are not distinguished. A domino may be placed horizontally or vertically, and it may straddle the border between two squares.
Cell positions are written as follows. Rows are labeled to from top to bottom, columns are labeled to from left to right, and a cell position is written as (row letter)(column digit). For example, denotes row , column .
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 of dominoes already placed ().
Each of the next lines describes one domino in the format U LU V LV. is one digit written on the domino and is the position of the cell holding it (a string of length ); is the other digit on the domino and 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 through . 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 , marking the end of the input.
Output
For each puzzle, first print Puzzle k, where is its position in the input (starting from ). Then print the completed grid as nine lines of nine digits each.
Only inputs with a unique solution are given.