#P4576. Chessboard Duel: Capture the Opponent
Chessboard Duel: Capture the Opponent
Chessboard Duel: Capture the Opponent
Consider an \(n\times n\) chessboard (with \(n\ge2\)) on which there is a white piece and a black piece placed at distinct squares. Two players, A and B, control the white and black pieces respectively, and they move alternately with A moving first.
The rules are as follows:
- Player A (White): May only move the white piece by exactly one square in one of the four cardinal directions (up, down, left, right). Formally, if the white piece is at \((x,y)\), it may move to \((x+1,y)\), \((x-1,y)\), \((x,y+1)\) or \((x,y-1)\), provided the destination remains inside the board.
- Player B (Black): May only move the black piece. The allowed moves are to move one or two squares in one of the four cardinal directions. That is, if the black piece is at \((x,y)\), it may move to \((x+k,y)\) or \((x-k,y)\) or \((x,y+k)\) or \((x,y-k)\) for \(k=1\) or \(2\), again provided the destination is within the board.
The game is played by alternate moves. A capture (and immediate win) occurs when a player moves his own piece onto the square currently occupied by the opponent’s piece. Both players are assumed to be perfectly rational: if a winning strategy exists they will follow it to secure victory in the minimum number of moves (each move is counted as one round), and if they are destined to lose they will try to delay the win as long as possible.
Your task is: given \(n\) and the initial positions of the white and black pieces, determine which player will win and in how many rounds (i.e. the move number on which the game ends).
Note on Coordinates: The board coordinates are 1-indexed. For example, on a \(2\times2\) board, the only valid coordinates are \((1,1)\), \((1,2)\), \((2,1)\) and \((2,2)\).
Example: For \(n=2\), if the white piece is at \((1,1)\) and the black piece is at \((2,2)\), then though A (white) has two possible moves, no matter what A does, B can force a win on the second move (round 2). Thus, the answer is that player B wins in 2 rounds.
inputFormat
The first line contains an integer \(n\) (\(n\ge2\)). The second line contains two integers representing the row and column coordinates of the white piece. The third line contains two integers representing the row and column coordinates of the black piece.
It is guaranteed that the two pieces are in different squares, and all coordinates are between 1 and \(n\) (inclusive).
outputFormat
Print a line containing the winner and the number of rounds needed until the game ends. Use A
to denote player A (white) and B
to denote player B (black). The round count is the move number when the winning move is made.
For example, if player B wins on his first move (which is the second move overall), print:
B 2
sample
2
1 1
2 2
B 2