Under construction

Still being built. Things may change

Chocolate Fix

Time limit1sMemory limit1024 MB

Summary
Place the nine shape-flavor truffles on a 3 x 3 grid so that every clue window matches, then print the unique solution for each puzzle.
Level

Medium4 of 10

Topics
Brute force, Backtracking, Implementation
Solved
No attempts yet

Problem

Your cousin recently bought a game that involves arranging truffles on a 3 x 3 grid. Eager to one up him, you decide to write a program to solve the puzzles automatically, so that he doesn't have a chance in figuring out the puzzles against you!

The game comes with nine truffles, which are used in each puzzle. Each truffle is either vanilla, strawberry, or chocolate (V, S, C) in flavor, and either square, round, or triangular (S, R, T) in shape. There is exactly one truffle for each combination of attributes, and all nine must be used. In each puzzle, you must place each of the nine truffles on the 3 x 3 board. For convenience, the squares are numbered 1, 2, and 3 on the first row from left to right, 4, 5, and 6 on the second row, and 7, 8, and 9 on the third row, as shown below:

Each puzzle comes with a list of clues. Each clue gives partial information about a part of the board. For example, the clue below says that there is some 3 x 2 window whose upper right corner holds a square strawberry truffle:

__ SS
__ __
__ __

There are only two such 3 x 2 windows on a 3 x 3 board. So this clue tells you that the square strawberry truffle must be in either square 2 or square 3. Clues cannot be rotated.

Here is another clue:

__ _S
__ RC
__ T_

This clue also describes a 3 x 2 window. The right column of the window has a strawberry truffle (of any shape) in the top row, a round chocolate truffle in the middle row, and a triangular truffle (of any flavor) in the bottom row. Because of the window size, this must hold for either the middle column or the rightmost column of the board.

Given a set of clues that uniquely determines the solution to a chocolate puzzle, find that solution.

Input

The first line contains a positive integer n, the number of puzzles to solve. The puzzles follow. The first line of each puzzle contains a single positive integer c (1 ≤ c ≤ 10), the number of clues for that puzzle. The clues follow. The first line of each clue contains two space-separated integers x (1 ≤ x ≤ 3) and y (1 ≤ y ≤ 3), the number of rows and columns of the clue window. The next x lines give the clue, and each of these lines contains y codes separated by exactly one space. Each code has two characters. The first character is from {'','S','R','T'} and gives the shape. The second character is from {'','V','S','C'} and gives the flavor. An underscore means that attribute is not fixed.

Output

For each puzzle, the first output line is "Puzzle #p:", where p is the puzzle number starting at 1. The next three lines give the puzzle solution, using the same codes as the clues. Since each puzzle has a unique solution, no underscore appears in a valid solution. Leave a blank line after the output for each test case. Follow the format shown in the sample output.

Examples1

  1. Example 1

    Input
    3
    4
    3 3
    TC __ SS
    __ __ __
    __ TV __
    2 3
    __ SC __
    RV __ SV
    3 3
    __ __ __
    __ RC __
    __ __ __
    2 3
    __ __ __
    TS __ RS
    5
    2 3
    __ __ __
    __ __ RC
    2 2
    __ RS
    SC __
    2 2
    SV TC
    __ __
    3 2
    TV __
    __ __
    __ RV
    3 2
    __ TS
    __ __
    __ __
    3
    3 2
    _C R_
    _C __
    S_ _C
    1 2
    TC _V
    3 2
    _V __
    S_ S_
    T_ _V
    
    Expected output
    Puzzle #1:
    TC SC SS
    RV RC SV
    TS TV RS
    
    Puzzle #2:
    TV RS TS
    SC SV TC
    SS RV RC
    
    Puzzle #3:
    TV TC RV
    SS SC RS
    TS SV RC