#P13162. [GCJ 2017 #1A] Alphabet Cake

    ID: 15029 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>数学贪心2017递归Special JudgeGoogle Code Jam

[GCJ 2017 #1A] Alphabet Cake

题目描述

You are catering a party for some children, and you are serving them a cake in the shape of a grid with RR rows and CC columns. Your assistant has started to decorate the cake by writing every child's initial in icing on exactly one cell of the cake. Each cell contains at most one initial, and since no two children share the same initial, no initial appears more than once on the cake.

Each child wants a single rectangular (grid-aligned) piece of cake that has their initial and no other child's initial(s). Can you find a way to assign every blank cell of the cake to one child, such that this goal is accomplished? It is guaranteed that this is always possible. There is no need to split the cake evenly among the children, and one or more of them may even get a 1-by-1 piece; this will be a valuable life lesson about unfairness.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each begins with one line with two integers RR and CC. Then, there are RR more lines of CC characters each, representing the cake. Each character is either an uppercase English letter (which means that your assistant has already added that letter to that cell) or ? (which means that that cell is blank).

输出格式

For each test case, output one line containing Case #x: and nothing else. Then output RR more lines of CC characters each. Your output grid must be identical to the input grid, but with every ? replaced with an uppercase English letter, representing that that cell appears in the slice for the child who has that initial. You may not add letters that did not originally appear in the input. In your grid, for each letter, the region formed by all the cells containing that letter must be a single grid-aligned rectangle.

3
3 3
G??
?C?
??J
3 4
CODE
????
?JAM
2 2
CA
KE
Case #1:
GGJ
CCJ
CCJ
Case #2:
CODE
COAE
JJAM
Case #3:
CA
KE

提示

Sample Explanation

The sample output displays one set of answers to the sample cases. Other answers may be possible.

Limits

  • 1T1001 \leq T \leq 100.
  • There is at least one letter in the input grid.
  • No letter appears in more than one cell in the input grid.
  • It is guaranteed that at least one answer exists for each test case.

Small dataset (8 Pts, Test Set 1 - Visible)

  • 1R121 \leq R \leq 12.
  • 1C121 \leq C \leq 12.
  • R×C12R \times C \leq 12.

Large dataset (13 Pts, Test Set 2 - Hidden)

  • 1R251 \leq R \leq 25.
  • 1C251 \leq C \leq 25.