Under construction

Still being built. Things may change

Alphabet Cake (Small)

Interview

Time limit5sMemory limit512 MB

Summary
Assign every blank cell of a small grid to an existing letter so each letter occupies one rectangle, choosing the lexicographically smallest result.
Level

Medium6 of 10

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

Problem

You are catering a children's party with a cake shaped like a grid of RR rows and CC columns. Your assistant has written the initial of one child in icing on one cell of the cake, for each child. A cell holds at most one initial, and no two children share an initial, so no letter appears twice.

Each child wants a single rectangular piece that contains their own initial and no other child's initial. The sides of a piece follow the grid lines. Assign every blank cell to one child so that this works out, leaving no cell unassigned. Only inputs that admit such an assignment are given. The cake does not have to be split evenly, and a child may end up with a 1×11 \times 1 piece.

Input

The first line contains the number of test cases TT. TT test cases follow. The first line of each test case contains two integers RR and CC separated by a space. Then RR lines of CC characters each describe the cake. Each character is either an uppercase English letter or ?. An uppercase letter is an initial already written on that cell, and ? is a blank cell.

Limits:

  • 1≤T≤1001 \le T \le 100
  • 1≤R≤121 \le R \le 12
  • 1≤C≤121 \le C \le 12
  • R×C≤12R \times C \le 12
  • The input grid contains at least one letter.
  • No letter appears in more than one cell.
  • Every test case admits at least one valid assignment.

Output

For each test case, first output one line containing Case #x: and nothing else, where xx is the test case number starting from 1. Then output RR more lines of CC characters each. The output grid must be the input grid with every ? replaced by an uppercase letter that names the child whose piece contains that cell. You may not write a letter that does not appear in the input. For each letter, the cells holding that letter must form one rectangle whose sides follow the grid lines.

Several grids can satisfy those rules, and only one of them is accepted. Read each row left to right and concatenate the rows from top to bottom into a string of length R×CR \times C. Output the grid whose string is the lexicographically smallest.

Examples3

  1. Example 1

    Input
    3
    3 3
    G??
    ?C?
    ??J
    3 4
    CODE
    ????
    ?JAM
    2 2
    CA
    KE
    
    Expected output
    Case #1:
    GCC
    GCC
    GJJ
    Case #2:
    CODE
    CJAE
    CJAM
    Case #3:
    CA
    KE
    
  2. Example 2

    Input
    2
    1 1
    A
    3 4
    ????
    ?Z??
    ????
    
    Expected output
    Case #1:
    A
    Case #2:
    ZZZZ
    ZZZZ
    ZZZZ
    
  3. Example 3

    Input
    2
    3 4
    ABCD
    EFGH
    IJKL
    2 2
    ZY
    XW
    
    Expected output
    Case #1:
    ABCD
    EFGH
    IJKL
    Case #2:
    ZY
    XW