#P10543. [THUPC 2024 决赛] 黑白
[THUPC 2024 决赛] 黑白
Problem Description
Xiao I and Xiao J are playing a game again.
The game is played on an grid. We use an ordered pair to describe the cell in row and column , where . Initially, at least one cell on the grid is white, and all other cells are black.
Xiao I and Xiao J take turns to make moves, with Xiao I moving first. In each move, the player must choose exactly one white cell and paint it black.
Two cells are considered adjacent if and only if they share an edge. If after some move, cell is not white, or cell is not white, or it is impossible to start from and reach by moving each time to an adjacent white cell (that is, and are not in the same 4-connected component formed by white cells), then the current player loses and the other player wins.
As a spectator, you want to know who will win if both Xiao I and Xiao J play perfectly.
Input Format
This problem contains multiple test cases. The first line contains an integer , denoting the number of test cases. Then the test cases follow. For each test case:
- The first line contains two integers , denoting the size of the grid.
- The next lines each contain a string of length over the character set
BW, describing the initial coloring of the grid. The -th character in the -th line isBif is initially black, otherwise it isWmeaning is initially white. It is guaranteed that there is at least one white cell on the initial board.
It is guaranteed that, within a single test point, the sum of over all test cases does not exceed .
Output Format
For each test case, output one character in one line. If Xiao I has a winning strategy, output I; otherwise, if Xiao J has a winning strategy, output J.
2
2 2
WB
WW
2 2
WW
WW
J
I
Hint
For the first test case, Xiao I can only paint black, but painting black will make it impossible to move from to by moving only to adjacent white cells each time. Therefore, no matter how Xiao I plays, Xiao I will lose, so output J.
For the second test case, Xiao I can paint black, and then no matter how Xiao J plays, Xiao J will lose, so output I.
Source and Acknowledgements
From the finals of THUPC2024 (the 2024 Tsinghua University Student Programming Contest and Intercollegiate Invitational Contest). Thanks to THUSAA for providing the problem.
For the testdata, statement, reference solution, editorial, etc., please refer to the official THUPC repository at https://thusaac.com/public.
Translated by ChatGPT 5