#P4658. [BalticOI 2008] 游戏 (Day1)

[BalticOI 2008] 游戏 (Day1)

Description

Player A\text{A} and player B\text{B} play a game on an n×nn \times n square grid. Each cell on the board is either white or black. The game is played only on the white cells; black cells are forbidden. Initially, each player has one piece placed at their own starting cell. It is guaranteed that the two starting cells are different.

The players move alternately, with player A\text{A} moving first. In each move, a player moves their piece to an adjacent white cell. If a player moves their piece onto the cell currently occupied by the opponent, the player may move one more step to jump over the opponent. Note that in this case, the direction of the second step may be different from the first step.

The goal of the game is to be the first player whose piece reaches the opponent’s starting cell. Even if the player’s last move consists of two steps and they only jump over the opponent’s starting cell (if the opponent is currently on that starting cell), the player still wins.

Given the board layout and the two players’ starting positions, determine which player has a winning strategy (a player has a winning strategy if they can win no matter how the opponent moves).

Input Format

The first line of standard input contains a positive integer tt (1t101 \le t \le 10), the number of test cases. Then follow tt test cases. Each test case is given as follows:

The first line of a test case contains a positive integer nn (2n3002 \le n \le 300), the side length of the grid. Each of the next nn lines contains nn characters (with no spaces). Each character is one of . (a white cell), # (a black cell), A (the starting cell of A\text{A}), and B (the starting cell of B\text{B}).

It is guaranteed that there exists a path of white cells between the two starting cells.

Output Format

For each test case, output one line to standard output containing one character, A or B, indicating the player who has a winning strategy.

2
4
A...
.#..
....
...B
4
A...
....
..#.
...B
B
A

Hint

Sample Explanation

For the first test case:

0

If A\text{A} moves to the right edge of the grid within the first three moves, then B\text{B} will move upward within the first three moves. Therefore, on the third move, player B\text{B} will reach the cell where A\text{A} is and gain an extra move. Thus, B\text{B} will reach A\text{A}’s starting cell first and win the game.

For the second test case:

1

A\text{A} can first move one step to the right and then one step down. After that, A\text{A} can decide whether to move down or right based on B\text{B}’s first two moves, so as to avoid B\text{B}. In this way, A\text{A} will reach B\text{B}’s starting cell first and win the game. In fact, we have already proven that A\text{A} has a winning strategy.

Constraints

For 40%40\% of the testdata, n40n \le 40.

For 60%60\% of the testdata, n150n \le 150.

For all testdata, 2n3002 \le n \le 300.

Translated by ChatGPT 5