#P13121. [GCJ 2019 #3] Datacenter Duplex

    ID: 12905 Type: RemoteJudge 20000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2019并查集Special JudgeGoogle Code Jam

[GCJ 2019 #3] Datacenter Duplex

题目描述

Two companies, Apricot Rules LLC and Banana Rocks Inc., are sharing the same datacenter. The datacenter is a matrix of R\mathbf{R} rows and C\mathbf{C} columns, with each cell containing a single server tower. Each tower contains intellectual property belonging to exactly one of the two companies.

At first, they built walls on the edges between cells assigned to different companies. This allowed orthogonally adjacent cells belonging to the same company to remain connected. Also, two cells x\mathbf{x} and y\mathbf{y} are considered connected if x\mathbf{x} is connected to a cell that is, directly or indirectly, connected to y\mathbf{y}. With this definition, it was possible that two cells assigned to the same company were not connected, which was unacceptable.

Both companies agreed to build narrow hallways running through cell corners that allow two diagonally adjacent cells to be connected directly. Let us write (i,j)(\mathbf{i}, \mathbf{j}) to represent the cell at row i\mathbf{i} and column j\mathbf{j}. At most one narrow hallway can be built through any given vertex, which means either (i,j)(\mathbf{i}, \mathbf{j}) and (i+1,j+1)(\mathbf{i} + 1, \mathbf{j} + 1) can be connected, or (i+1,j)(\mathbf{i} + 1, \mathbf{j}) and (i,j+1)(\mathbf{i}, \mathbf{j} + 1) can be connected, or neither pair, but not both. Of course, only hallways between cells assigned to the same company can be built.

Given a matrix where each cell is labeled A\mathbf{A} or B\mathbf{B} depending on which company it is assigned to, find a way to add connections through diagonal adjacencies such that all A\mathbf{A}s are connected and all B\mathbf{B}s are connected.

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case begins with one line containing two integers R\mathbf{R} and C\mathbf{C}, the number of rows and columns of the matrix representing the datacenter. Then, there are R\mathbf{R} more lines containing C\mathbf{C} characters each. The j\mathbf{j}-th character on the i\mathbf{i}-th of these lines $\mathbf{M}

输出格式

For each test case, first output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is IMPOSSIBLE if there is no way to assign the diagonal connections such that the A\mathbf{A} cells are connected and the B\mathbf{B} cells are connected, or POSSIBLE otherwise. Then, if you output POSSIBLE, output R1\mathbf{R} - 1 more lines of C1\mathbf{C} - 1 characters each. These characters must correspond to a valid arrangement as described in the statement above. The j\mathbf{j}-th character of the i\mathbf{i}-th of those lines must be \\backslash if cells (i,j)(\mathbf{i}, \mathbf{j}) and (i+1,j+1)(\mathbf{i} + 1, \mathbf{j} + 1) are to be connected, / if cells (i+1,j)(\mathbf{i} + 1, \mathbf{j}) and (i,j+1)(\mathbf{i}, \mathbf{j} + 1) are to be connected, or . if neither pair is to be connected.

3
2 2
AB
BA
2 3
AAB
ABB
3 4
BBAA
BABA
BBAA
Case #1: IMPOSSIBLE
Case #2: POSSIBLE
..
Case #3: POSSIBLE
//\
.//

提示

Sample Explanation

In Sample Case #1, the pair of A cells and the pair of B cells need to be connected, but since both connections would have to cross the same vertex, at most one of the connections can exist.

In Sample Case #2, the cells are already connected in the required way in the input, so no additional connections are necessary. Note that you can add unnecessary valid connections, so another valid answer would be //, but \. would be wrong.

In Sample Case #3, there are also multiple solutions, one of which is displayed.

Limits

  • 1T1001 \leq \mathbf{T} \leq 100.
  • 2C1002 \leq \mathbf{C} \leq 100.
  • Mi,j\mathbf{M}_{\mathbf{i}, \mathbf{j}} = uppercase A or uppercase B, for all i\mathbf{i} and j\mathbf{j}.
  • Mi,j\mathbf{M}_{\mathbf{i}, \mathbf{j}} = uppercase A for at least one pair of i\mathbf{i} and j\mathbf{j}.
  • Mi,j\mathbf{M}_{\mathbf{i}, \mathbf{j}} = uppercase B for at least one pair of i\mathbf{i} and j\mathbf{j}.

Test set 1 (10 Pts, Visible)

  • 2R42 \leq \mathbf{R} \leq 4.

Test set 2 (13 Pts, Hidden)

  • 2R1002 \leq \mathbf{R} \leq 100.