#P16855. [GKS 2021 #E] Palindromic Crossword
[GKS 2021 #E] Palindromic Crossword
Problem Description
A crossword puzzle is a rectangular grid of black cells and letters A-Z like the one shown below.
:::align{center}
:::
Words in the crossword are defined as maximal vertical or horizontal segments of characters. In the crossword below, DO and ON are examples of words.
:::align{center}
:::
A palindromic crossword is one where every word is a palindrome. Let represent the character on the -th row and -th column, where and are -indexed. The top left corner is . In the example palindromic crossword below, the B in is part of both the horizontal word starting at and the vertical word ending at , and both are palindromes.
:::align{center}
:::
You have been gifted a palindromic crossword puzzle with rows and columns. You finished the crossword and throw away the clues, preparing to hang it on your wall. However, you accidentally erase some of the letters! You want to recover as much of the crossword as possible, but you do not have the clues anymore. Using only the knowledge that the crossword is palindromic, restore the maximum possible number of missing characters in the given crossword.
Missing letters are represented as empty white cells in the below diagram. The crossword on the left is the crossword you are given and the crossword on the right is the result after you recover as many letters as possible. The remaining cells cannot be filled because we do not have sufficient information to recover them.
:::align{center}
:::
Input Format
The first line of the input gives the number of test cases, . test cases follow.
The first line of each test case contains two integers, and , representing the number of rows and columns in the crossword, respectively.
The next lines represent the rows of the grid. The -th row consists of characters representing , , ..., . Each character is one of the following:
- A capital letter of the alphabet (-)
- A period (
.) for a missing letter (empty white cell in the example crossword) - A hash (
#) for black cell
Output Format
For each test case, output one line containing Case #: where is the test case number (starting from ) and is the number of empty white cells that were filled. Then, output more lines representing the final grid, with the missing characters (.) replaced by capital letters (-) where possible.
2
2 2
A.
.#
4 6
A...#.
B##...
B.###.
A...#.
Case #1: 2
AA
A#
Case #2: 8
A..A#.
B##A.A
BB###A
ABBA#.
Hint
In Sample Case #, we are able to fill in of the blanks. We can fill in the missing letters as follows:
- row , column : We know this is
Afrom character at row , column . - row , column =
Afrom row , column . - row , column =
Afrom row , column . - row , column =
Afrom row , column . - row , column =
Bfrom row , column . - row , column =
Bfrom row , column . - row , column =
Bfrom row , column . - row , column =
Afrom row , column .
Limits
.
There exists at least one way to fill in the given input grid such that it is a palindromic crossword.
All characters in the grid are in the set .
Test Set 1
.
Test Set 2
For at most cases:
.
For the remaining cases:
.