atcoder#AGC029D. [AGC029D] Grid game
[AGC029D] Grid game
Score : points
Problem Statement
Takahashi and Aoki will play a game using a grid with rows and columns of square cells. There are obstacles on this grid; the -th obstacle is at . Here, we represent the cell at the -th row and -th column by . There is no obstacle at , and there is a piece placed there at .
Starting from Takahashi, he and Aoki alternately perform one of the following actions:
- Move the piece to an adjacent cell. Here, let the position of the piece be . Then Takahashi can only move the piece to , and Aoki can only move the piece to . If the destination cell does not exist or it is occupied by an obstacle, this action cannot be taken.
- Do not move the piece, and end his turn without affecting the grid.
The game ends when the piece does not move twice in a row.
Takahashi would like to perform as many actions (including not moving the piece) as possible before the game ends, while Aoki would like to perform as few actions as possible before the game ends. How many actions will Takahashi end up performing?
Constraints
- If ,
- and are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of actions Takahashi will end up performing.
3 3 1
3 2
2
For example, the game proceeds as follows:
- Takahashi moves the piece to (2,1).
- Aoki does not move the piece.
- Takahashi moves the piece to (3,1).
- Aoki does not move the piece.
- Takahashi does not move the piece.
Takahashi performs three actions in this case, but if both players play optimally, Takahashi will perform only two actions before the game ends.
10 10 14
4 3
2 2
7 3
9 10
7 7
8 1
10 10
5 4
3 4
2 8
6 4
4 4
5 8
9 2
6
100000 100000 0
100000