#P4997. Random Simulation in Un-Go
Random Simulation in Un-Go
Random Simulation in Un-Go
In this problem, you are given an (n \times n) board representing a simplified variant of Go, called "Un-Go". The board may already contain some stones, and the initial configuration is legal: no connected group of stones (stones of the same color connected orthogonally) has zero liberties. Moreover, the number of black and white stones are equal. Hence, it is Black’s turn to move.
A move consists of placing a stone of the current player on an empty cell subject to the following rule (all rules below are expressed in (\LaTeX) format):
-
When a stone is placed at cell ((i,j)), the move is simulated as follows: [ \text{Place the stone } s \text{ (either }B\text{ or }W\text{) at } (i,j). ]
-
For each of the four directions (up, down, left, right), if there is an opponent stone adjacent to ((i,j)), consider the connected group (of opponent stones) that this stone belongs to. A group is defined as a collection of stones of the same color connected orthogonally. A group has a liberty if at least one of its adjacent cells is empty. Formally, if a group (G) does not have any adjacent empty cell (liberty), i.e., [ \nexists \ (x,y) \text{ adjacent to any stone in } G \text{ such that the cell is empty}, ] then remove all stones in group (G) from the board.
-
After performing the removals, check the connected group of the newly placed stone. If this group has no liberties (i.e. there is no empty cell adjacent to any stone in the group), then the move is illegal (suicide is forbidden).
The simulation proceeds by letting Black and White move alternately. In each turn, the program selects an arbitrary legal move for the current player (you can choose the first legal move in row-major order). The simulation stops as soon as the current player cannot make any legal move. In that case, the opponent wins the game.
Your task is to implement this random simulation. At the end, print a single line declaring the winner. Print "Black" if Black wins, or "White" if White wins.
inputFormat
The first line contains an integer (n) ((1 \le n \le 20)), the size of the board. The next (n) lines each contain a string of length (n). Each character is one of:
- '.' (a dot) representing an empty cell
- 'B' representing a black stone
- 'W' representing a white stone
It is guaranteed that the initial board is legal (every stone or connected group of stones has at least one liberty) and that the numbers of black and white stones are equal.
outputFormat
Output a single line containing the winner of the simulation. If the winning player's color is Black, print "Black"; otherwise, print "White".
sample
3
...
...
...
Black