#P10501. Cutting Game Strategy
Cutting Game Strategy
Cutting Game Strategy
In this game a rectangular paper of size \(W \times H\) (with exactly \(W\) rows and \(H\) columns of unit cells) is given. Two players take turns to cut one of the pieces of paper. Initially there is one piece, the \(W\times H\) paper. In each turn, the current player chooses one piece that is at least 2 in one dimension and makes a straight cut (either horizontal or vertical) along the grid lines, splitting that piece into two smaller rectangular pieces. All cuts must go along the grid lines and cannot break a single grid.
A move is winning if the cut produces a piece that is exactly a single grid cell (a \(1\times1\) rectangle). Note that such a winning move can only be made if the chosen piece is a strip (i.e. its one dimension is 1 and the other is greater than 1) so that by a cut at the only interior grid line the cutter creates a \(1\times1\) piece. If a player makes a winning move, the game ends immediately and that player wins.
If no winning move is made during the game, the players will continue to make cuts until a total of \(N\) turns have been taken; at that point the paper is cut into \(N+1\) pieces and the game ends without a win. Both players play optimally: in a turn they will choose a winning move if one is available, and otherwise they will avoid moves that allow the opponent to win on the subsequent turn. In other words, if a cut would create a strip
piece (either a \(1\times L\) or \(L\times1\) rectangle with \(L>1\)) then the opponent on his next turn could cut that strip to get a winning \(1\times1\) piece. Such moves are considered "unsafe" and will be avoided if there is any alternative.
Your task is: given \(W, H\) and \(N\), decide whether the first (starting) player can force a win (i.e. guarantee making a winning move) within \(N\) turns under optimal play.
Note on moves:
- If the current paper is a strip (i.e. it is \(1\times L\) with \(L>1\) or \(L\times1\) with \(L>1\)) then a winning move exists immediately: by making a cut so that one of the two pieces is \(1\times1\) the current player wins.
- If the current paper has both dimensions at least 2 and there is a cut that avoids creating a strip in any piece, we call that a safe move. (For a horizontal cut of an \(a\times b\) rectangle, cutting between row \(k\) and \(k+1\) is safe only if neither \(k=1\) nor \(a-k=1\) when \(b>1\); similarly for vertical cuts.)
- If no safe move exists from a given configuration, then whichever move is made will create a strip and allow the opponent a winning move on his turn.
Input format:
W H N
Output format:
YES
if the first player can force a win; otherwise output
NO
Constraints and details:
- \(1 \le W,H \le 10\) (small sizes to allow recursion over game states).
- \(1 \le N \le 10\).
- You may assume that when \(W\) or \(H\) equals 1 and the other is at least 2, a winning move exists immediately.
inputFormat
The input consists of a single line containing three space‐separated integers \(W\), \(H\) and \(N\):
W H N
where \(W\) and \(H\) represent the number of rows and columns of the paper, and \(N\) is the maximum number of turns allowed.
outputFormat
Output a single line: YES
if the first player can force a win under optimal play within \(N\) turns, or NO
otherwise.
sample
1 3 1
YES