#P7479. Determining the Life Status of a Black Go Group
Determining the Life Status of a Black Go Group
Determining the Life Status of a Black Go Group
Consider an Go board. Each cell is either empty or contains a black stone. It is guaranteed that all black stones form a single connected group (i.e. for any two black stones and there exists a path through adjacent (Manhattan distance 1) black stones).
In Go, the liberties of a stone are defined as the set of all empty positions adjacent (up, down, left, right) to it. The liberties of a connected group of stones is the union of the liberties of all stones in that group.
A white move is legal if and only if, after placing a white stone in an empty cell, either:
- The connected white group (formed by that stone and any adjacent white stones) has at least one liberty, or
- The black group’s liberties become zero immediately (i.e. the move captures the black group).
We say that the black group is "alive" (i.e. an \emph{alive group}) if, no matter how many consecutive legal moves white plays, the black group always maintains at least one liberty. Otherwise, it is not alive.
Your task is to determine whether the black group is alive. Output YES if it is alive, and NO otherwise.
\textbf{Note:} A key observation is that white is trying to fill all the liberties of the black group. However, due to the legality rule, white cannot fill an isolated empty cell (i.e. a component of liberties of size exactly ) as an intermediate move because such a move would be suicidal (unless it is the final move that captures the entire group). Thus, if there exist at least two isolated liberty cells (components of size ) among the black group’s liberties, then white cannot fill them both, and the black group is considered alive.
inputFormat
The first line contains two integers and (), representing the number of rows and columns of the board.
Each of the following lines contains a string of length consisting of the characters 'B'
and '.'
, where 'B'
denotes a black stone and '.'
denotes an empty cell. It is guaranteed that the black stones form a single connected group.
outputFormat
Output a single line with either YES
if the black group is alive or NO
if it is not.
sample
3 3
...
.B.
...
YES