Baltic OI '08 P1 - Game

View as PDF

Submit solution

Points: 12 (partial)
Time limit: 1.0s
Memory limit: 8M

Problem type
Baltic Olympiad in Informatics: 2008 Day 1, Problem 1

Two players, A and B, play a game on a square board of size n \times n. The squares of the board are either white or black. The game is played only on the white squares — the black ones are excluded from the game. Each player has one piece, initially placed at this player's starting point — one of the white squares on the board. The starting point of A is different than that of B.

In each move a player moves his piece to one of the neighboring white squares (either up, down, left or right). If the player moves his piece to the square currently occupied by his opponent's piece, he gets an extra move (this way he may jump over the opponent). Note that in this case the direction of the second move can be different than that of the first move.

Player A moves first, then players alternate. The goal of the game is to reach the opponent's starting point. The player whose piece reaches his opponent's starting point first, wins the game. We want to determine which player has a winning strategy (a player has a winning strategy if he can win regardless of his opponent's moves).

Figure 1. If A moves to the right on his first three moves, B will move up the first three moves. Thus, on the third move player B will reach the square with A's piece and will be allowed to move again. Because of this, B will reach A's starting point first and will win the game.

Figure 2. A can start by moving one step to the right and one step down. Then, depending on the first two moves of B, he will either go down or right and evade B. This way A will reach B's starting point first, thus winning the game. In fact we proved that A has a winning strategy.


1 \le t \le 10

2 \le n \le 300

Subtask 1 [40%]

2 \le n \le 40

Subtask 2 [20%]

2 \le n \le 150

Subtask 3 [40%]

No additional constraints.

Input Specification

The first line of the standard input contains one integer t, the number of test cases.

Each test is described as follows. In the first line of the test there is one integer n, the length of the side of the grid. Then next n lines contain the description of the grid. Each line consists of n characters (with no white-spaces between them). Each character is either . (a white square), # (a black square), A (the starting point of A) or B (the starting point of B). You may assume that there exists a path of white squares between the starting points of A and B.

Output Specification

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

Sample Input


Sample Output


Sample Explanation

See Figure 1 and 2 above.


There are no comments at the moment.