#P9609. Polluted Chocolate Game
Polluted Chocolate Game
Polluted Chocolate Game
The game starts with a classic rectangular chocolate block of size m \times n divided into small squares by its grid lines. Some of these squares are contaminated with a bitter substance that cannot be digested. Two players take turns making a move. In each move, a player must split one chocolate block along one of its grid grooves into two smaller non-empty blocks and then eat exactly one of the pieces. If the piece that is eaten contains at least one contaminated square, the player loses immediately.
A winning move in this problem is defined as a move in which the player splits the current block so that one of the two pieces is completely safe (i.e. contains no contaminated square) and the other piece is contaminated (i.e. contains at least one contaminated square), and then the player eats the safe piece. As a result, the remaining block passed to the opponent is contaminated, forcing them into an eventual loss if both play optimally.
The allowed splits are either horizontal or vertical along a grid line:
- For a horizontal split at row i (with 1 \le i < m): the top block covers rows 1 to i and the bottom block covers rows i+1 to m. If the top block is safe and the bottom block is contaminated, output
H TOP i
; if the bottom block is safe and the top block is contaminated, outputH BOTTOM i
. - For a vertical split at column j (with 1 \le j < n): the left block covers columns 1 to j and the right block covers columns j+1 to n. If the left block is safe and the right block is contaminated, output
V LEFT j
; if the right block is safe and the left block is contaminated, outputV RIGHT j
.
If no winning move exists, your program should simply output 0
.
Note: The contaminated squares are given from the start. The board is 1-indexed (i.e. rows and columns are numbered from 1). A safe block is one that contains 0 contaminated squares, while a contaminated block has at least one.
inputFormat
The first line contains two integers (m) and (n), representing the number of rows and columns of the chocolate block. The second line contains a single integer (k), the number of contaminated squares. Each of the following (k) lines contains two integers (r) and (c), indicating that the square in row (r) and column (c) is contaminated.
outputFormat
If a winning move exists, print one winning move in one of the following formats (without quotes):\
- For a horizontal split: either "H TOP i" (if the top piece, consisting of rows 1 to (i), is safe) or "H BOTTOM i" (if the bottom piece, consisting of rows (i+1) to (m), is safe).\
- For a vertical split: either "V LEFT j" (if the left piece, consisting of columns 1 to (j), is safe) or "V RIGHT j" (if the right piece, consisting of columns (j+1) to (n), is safe).
If no winning move exists, output a single 0.
sample
3 3
1
2 2
H TOP 1