No.3598 Queen vs. King
タグ : / 解いたユーザー数 47
作問者 : 👑
ぽえ
Problem Statement
This is an interactive problem (a problem in which your program and the judge program interact through standard input and output).
There is a grid with $H$ rows and $W$ columns. The square at the $i(1 \leq i \leq H)$-th row from the top and the $j(1 \leq j \leq W)$-th column from the left is denoted by square $(i, j)$.
Initially, a queen piece is on square $(1, 1)$ and a king piece is on square $(H, W)$. The queen piece and the king piece can freely enter and leave squares within the grid, but cannot enter or leave squares outside the grid.
Alice and Bob will now play the following game. Starting with Bob, Alice and Bob alternately perform the operations described below.
- Alice chooses $1$ square that is reachable in $1$ move from the square the queen piece is currently on and that does not contain the king piece, and moves the queen piece to that square.
- Bob chooses $1$ square that is reachable in $1$ move from the square the king piece is currently on, that does not contain the queen piece, and that is also not reachable by the queen piece in $1$ move, and moves the king piece to that square.
If on Bob's turn there is no square to which the king piece can move, the game ends. Otherwise, the game continues.
Your program acts as Alice, and the judge program acts as Bob.
Alice's goal is to end the game within $3$ of her own turns. Move the piece appropriately so that the game ends within $3$ of your own turns.
You are given $T$ test cases; solve each of them.
How the queen piece moves (click to open)
In $1$ move, the queen piece can advance any number of squares in the vertical, horizontal, or diagonal directions, as long as all squares on its path can be entered and left. However, staying on the current square does not count as $1$ move. Formally, a queen piece on square $(i, j)$ can move as follows.
- For a positive integer $k$, it can move to square $(i, j + k)$ if and only if the following condition is satisfied.
- For every integer $l$ satisfying $1 \leq l \leq k$, square $(i, j + l)$ is a square that the queen piece can enter and leave.
- For a positive integer $k$, it can move to square $(i, j - k)$ if and only if the following condition is satisfied.
- For every integer $l$ satisfying $1 \leq l \leq k$, square $(i, j - l)$ is a square that the queen piece can enter and leave.
- For a positive integer $k$, it can move to square $(i + k, j)$ if and only if the following condition is satisfied.
- For every integer $l$ satisfying $1 \leq l \leq k$, square $(i + l, j)$ is a square that the queen piece can enter and leave.
- For a positive integer $k$, it can move to square $(i - k, j)$ if and only if the following condition is satisfied.
- For every integer $l$ satisfying $1 \leq l \leq k$, square $(i - l, j)$ is a square that the queen piece can enter and leave.
- For a positive integer $k$, it can move to square $(i + k, j + k)$ if and only if the following condition is satisfied.
- For every integer $l$ satisfying $1 \leq l \leq k$, square $(i + l, j + l)$ is a square that the queen piece can enter and leave.
- For a positive integer $k$, it can move to square $(i + k, j - k)$ if and only if the following condition is satisfied.
- For every integer $l$ satisfying $1 \leq l \leq k$, square $(i + l, j - l)$ is a square that the queen piece can enter and leave.
- For a positive integer $k$, it can move to square $(i - k, j + k)$ if and only if the following condition is satisfied.
- For every integer $l$ satisfying $1 \leq l \leq k$, square $(i - l, j + l)$ is a square that the queen piece can enter and leave.
- For a positive integer $k$, it can move to square $(i - k, j - k)$ if and only if the following condition is satisfied.
- For every integer $l$ satisfying $1 \leq l \leq k$, square $(i - l, j - l)$ is a square that the queen piece can enter and leave.
How the king piece moves (click to open)
In $1$ move, the king piece can move to an adjacent square in the vertical, horizontal, or diagonal directions that can be entered and left. More specifically, a king piece on square $(i, j)$ can move to each of the squares $(i + 1, j), (i + 1, j + 1), (i, j + 1), (i - 1, j + 1), (i - 1, j), (i - 1, j - 1), (i, j - 1), (i + 1, j - 1)$, provided that the square can be entered and left.
Constraints
- All inputs are integers
- $1 \leq T \leq 1000$
- $3 \leq H, W \leq 100$
Input and Output
This is an interactive problem (a problem in which your program and the judge program interact through standard input and output).
Your program acts as Alice, and the judge program acts as Bob.
First, the number of test cases $T$ is given.
$T$
For each case, $H, W$ are first given from standard input.
$H$ $W$
Then, on Bob's turn, the content of his operation is given from standard input.
$x$ $y$
Here,
- If $(x, y) \neq (0, 0), (x, y) \neq (-1, -1)$, Bob moved the king piece to $(x, y)$ on his turn. This move follows the rules in the problem statement.
- If $(x, y) = (0, 0)$, Bob could not move the king piece on his turn, and the game has ended. In this case, proceed to the next test case if there is one; otherwise, terminate your program.
- If $(x, y) = (-1, -1)$, it means that an invalid output (including a move that does not follow the rules) was made on Alice's turn, or that the game had not ended after Alice finished $3$ turns. In this case, your submission has already been judged as incorrect, so terminate your program immediately.
On Alice's turn, output the square $(x, y)$ to which the queen piece is moved on that turn, in the following format.
$x$ $y$
This move must follow the rules.
Notes
- Be sure to flush standard output after every output. Otherwise, you may get TLE.
- If an invalid output is made, the judge result is undefined.
- If your program does not terminate immediately after $(x, y) = (-1, -1)$ is given as input, the judge result is undefined.
- After answering all test cases, terminate your program immediately. Otherwise, the judge result is undefined.
Sample
The following shows a sequence of input and output that is judged as correct.
| Input | Output | Explanation |
|---|---|---|
1 |
$T = 1$. | |
3 4 |
$H = 3, W = 4$. | |
2 4 |
Bob's move $1$. The king piece, initially on square $(3, 4)$, was moved to square $(2, 4)$. | |
2 2 |
Alice's move $1$. The queen piece on square $(1, 1)$ is moved to square $(2, 2)$. Note that the queen piece may be moved to a square that the king piece can reach in $1$ move. |
|
3 4 |
Bob's move $2$. The king piece on square $(2, 4)$ was moved to square $(3, 4)$. | |
1 2 |
Alice's move $2$. The queen piece on square $(2, 2)$ is moved to square $(1, 2)$. | |
2 4 |
Bob's move $3$. The king piece on square $(3, 4)$ was moved to square $(2, 4)$. | |
2 3 |
Alice's move $3$. The queen piece on square $(1, 2)$ is moved to square $(2, 3)$. | |
0 0 |
Bob can no longer move the king piece, so the game has ended. All test cases have been answered, so terminate your program. |
A sequence of input and output that is judged as incorrect is also shown below.
| Input | Output | Explanation |
|---|---|---|
1 |
$T = 1$. | |
4 5 |
$H = 4, W = 5$. | |
3 5 |
Bob's move $1$. The king piece, initially on square $(4, 5)$, was moved to square $(3, 5)$. | |
2 2 |
Alice's move $1$. The queen piece on square $(1, 1)$ is moved to square $(2, 2)$. | |
3 4 |
Bob's move $2$. The king piece on square $(3, 5)$ was moved to square $(3, 4)$. | |
1 2 |
Alice's move $2$. The queen piece on square $(2, 2)$ is moved to square $(1, 2)$. | |
2 4 |
Bob's move $3$. The king piece on square $(3, 4)$ was moved to square $(2, 4)$. | |
2 3 |
Alice's move $3$. The queen piece on square $(1, 2)$ is moved to square $(2, 3)$. | |
-1 -1 |
After Alice's turn number $3$, Bob can still move the king piece, so this is judged as incorrect. In this case as well, terminate your program. |
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。