Domino Tiling
Time limit1sMemory limit128 MB
Cover a grid with pre-placed tiles and all given dominoes, then output the lexicographically smallest valid tiling and the count of other tilings.
- Level
Hard8 of 10
- Topics
- Backtracking, Dynamic programming, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
The aliens want to conquer the entire universe, so it is no surprise that their favorite game is played with dominoes. A domino is a tile with a digit from to written on each of its two halves. The game board is a rectangular grid, and every unit square also holds a digit from to .
Your task is to cover the whole board with a given set of dominoes. A domino may be placed on two adjacent squares only if the two digits on the domino equal the two digits written on those squares. A domino may be placed as is or rotated by , , or , so a domino with halves and fits any pair of adjacent squares whose digits are and in either arrangement. No two dominoes may overlap, and each supplied domino may be used at most once.
Some squares already hold pre-placed tiles; these must remain exactly where they are. You must place all of the supplied dominoes so that, together with the pre-placed tiles, they cover every square of the board.
Input
The input contains several game descriptions.
The first line of each description contains three space-separated integers: the board height , the board width , and the number of available dominoes , where , , at least one of and is even, , and .
The second line contains pairs of integers (that is, integers) giving the two digits on each available domino. No two dominoes are identical, even when one of them is rotated by ; in other words the unordered digit pairs are pairwise distinct. None of the pre-placed tiles appears among these dominoes.
Each of the next lines contains space-separated entries. The entry in row and column (, ) is either the capital letter X, meaning that square already holds a pre-placed tile, or a digit with .
Descriptions are separated by blank lines. The input ends with a line containing three zeros (0 0 0), which must not be processed.
Output
For each game, decide whether all dominoes can be placed so that, together with the pre-placed tiles, they cover the whole board.
If it is possible, output the board as an grid of characters, one row per line with no spaces, using:
[and]for the left and right halves of a horizontally placed domino,nandufor the upper and lower halves of a vertically placed domino,Xfor a square covered by a pre-placed tile.
Several valid tilings may exist. To make the answer unique, output the lexicographically smallest grid: read the grid row by row from top to bottom and, within each row, from left to right, forming one string, and compare characters by their ASCII value (so the order is X < [ < ] < n < u); output the tiling whose resulting string is smallest.
After the grid rows, print a single line with the number of other valid tilings, that is, the total number of valid tilings minus one.
If no valid tiling exists, print a single line containing the word impossible instead.
Print one blank line between consecutive game results.