#P9169. [省选联考 2023] 过河卒

    ID: 10290 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>博弈论各省省选2023O2优化广度优先搜索 BFS拓扑排序

[省选联考 2023] 过河卒

Background

There is a river-crossing pawn on a chessboard that needs to reach the finish line. The pawn can move one cell to the left, one cell to the right, or one cell forward. At the same time, there are two opponent pieces on the board that need to stop the pawn from reaching the finish line. These two pieces move like the general: they can move to an adjacent cell in one of the four directions (up, down, left, right). Therefore, this problem can also be called “General Blocks the River-Crossing Pawn”.

Problem Description

There is a chessboard with nn rows and mm columns. We use (i,j)(i,j) to denote the position at row ii, column jj. On the board there are some obstacles, one black piece, and two red pieces.

The rules of the game are as follows: Red moves first, Black moves second, and they alternate turns.

On each Red turn, Red may choose one red piece and move it by one step to an adjacent cell. Specifically, suppose the chosen piece is at (i,j)(i,j). Then it may move to one of (i1,j)(i-1,j), (i+1,j)(i+1,j), (i,j1)(i,j-1), (i,j+1)(i,j+1), as long as the destination is inside the board, has no obstacle, and is not occupied by the other red piece.

On each Black turn, Black may move its piece by one step in one of three directions. Specifically, suppose the black piece is at (i,j)(i,j). Then it may move to one of (i1,j)(i-1,j), (i,j1)(i,j-1), (i,j+1)(i,j+1), as long as the destination is inside the board and has no obstacle.

Before a player takes an action, if any of the following situations occurs, the game ends immediately, and the winner is determined by the following rules (higher priority comes first):

  • The black piece is on the first row. In this case, Black wins.

  • The black piece is on the same cell as one of the red pieces. In this case, the player who made the previous move wins.

  • The current player has no legal move. In this case, the other player wins.

Now assume both sides use optimal strategies and will not make moves that are unfavorable to themselves. That is:

  • If there exists a winning strategy, the player will choose, among all winning strategies, a move that minimizes the maximum number of moves required for the player to win later, no matter how the opponent plays.
  • If there is no winning strategy, but there exists a strategy that guarantees the player will not lose regardless of how the opponent plays, the player may choose any such non-losing strategy.
  • If there is no non-losing strategy, the player will choose, among all strategies, a move that maximizes the minimum number of moves required for the opponent to win later, no matter how the opponent plays.

If the winner still cannot be determined after 100100100100^{100^{100}} turns, the game is considered a tie. Please output the total number of moves made by both sides when the game ends, or determine that the game is a tie.

Input Format

This problem has multiple test cases.

The first line contains two integers id,T\text{id}, T, representing the test point ID and the number of test cases. In particular, id\text{id} is 00 for the samples.

Then follow TT test cases. Each test case is given in the following format:

The first line contains two positive integers n,mn, m, denoting the number of rows and columns of the board.

Then follow nn lines, each containing a string of length mm. The jj-th character of the ii-th line represents the state of position (i,j)(i,j) on the board.

Among these characters: ’.’\texttt{'.'} denotes an empty cell; ’#’\texttt{'\#'} denotes an obstacle; ’X’\texttt{'X'} denotes the black piece; ’O’\texttt{'O'} denotes a red piece.

It is guaranteed that there is exactly one black piece, exactly two red pieces, and the black piece is not on the first row.

Output Format

For each test case, output one line containing a string.

If the game is a tie, output "Tie"\texttt{"Tie"}.

If Red wins, output "Red t"\texttt{"Red t"}, where t\texttt{t} is the total number of moves made by both sides when the game ends. Obviously, this should be an odd number.

If Black wins, output "Black t"\texttt{"Black t"}, where t\texttt{t} is the total number of moves made by both sides when the game ends. Obviously, this should be an even number.

Note: output the string inside the double quotes, without the double quotes.

0 5
4 5
...#O
.#..#
#O#..
.#..X
3 3
#.#
O.O
.X.
3 3
O..
.#X
.O.
5 5
.....
.....
..O..
#..#.
O#.X.
9 9
...######
.#.......
.#######.
.#.#.....
.#O#.####
.#.#.....
.#######.
.#X......
.O.......

Black 0
Black 2
Black 2
Tie
Red 75

见附件中的 zu/zu2.in
见附件中的 zu/zu2.ans

Hint

[Sample 1 Explanation]

In the first test case, Red has no legal move on the first move, so Black wins.

In the second test case, no matter how Red moves on the first move, Black can make the black piece share a cell with a red piece on the next move.

In the third test case, no matter how Red moves on the first move, Black can move its piece upward by one cell to achieve victory.

In the fourth test case, one red piece cannot move. The other red piece can move within the third row to prevent the black piece from entering the first row. The black piece can also keep moving within the fifth row. If a red piece reaches the fifth row, the black piece can choose to escape from the other side.

In the fifth test case, the red piece on the last row can go around from the left side to catch the black piece. Note that the other red piece can move.

[Sample 2 Explanation]

Each test case in this sample satisfies the constraints of one of the test points 55 to 1313.

[Subtasks]

For all data, it is guaranteed that: 1T101 \leq T \leq 10, 2n102 \leq n \leq 10, 1m101 \leq m \leq 10, and id\text{id} equals the test point ID.

For each test case, it is guaranteed that there is exactly one black piece, exactly two red pieces, and the black piece is not on the first row.

  • Test points 141 \sim 4: It is guaranteed that either the game is a tie, or Red cannot move at the beginning.

  • Test points 565 \sim 6: It is guaranteed that n4n \geq 4. It is guaranteed that every cell in row n1n-1 is an obstacle, and there are no obstacles in other rows. It is guaranteed that the black piece is in the first n2n-2 rows, one red piece is in the first n2n-2 rows, and the other red piece is in row nn.

  • Test points 797 \sim 9: It is guaranteed that m=1m = 1.

  • Test points 101310 \sim 13: It is guaranteed that either the game is a tie, or there exists a strategy that can end the game within 99 moves.

  • Test points 142014 \sim 20: No special constraints.

Translated by ChatGPT 5