spoj#BGAME. Game on board
Game on board
English | Vietnamese |
Two players, A and B, play a game on a square board of size n×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 jumps 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. Even if the player’s last move consists of two jumps, and he only jumps over his opponent’s starting point (since it is occupied by his opponent), the player wins. We want to determine which player has a winning strategy (a player has a winning trategy if he can win regardless of his opponent’s moves).
Task
Write a program, that:
- reads the layout of the grid and the starting points of the two players from the standard input,
- finds the player who has a winning strategy,
- writes the result to the standard output.
Input
The first line of the standard input contains one integer t the number of test cases (1 ≤ t ≤ 10 ). After it the description of t tests appears. Each test is described as follows. In the first line of the test there is one integer n (2 ≤ n ≤ 300 ), 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 whitespaces 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
For each test case exactly one line should be printed to standard output containing a single character ’A’ or ’B’, indicating the player who has a winning strategy.
Example
Input: 2 4 A... .#.. .... ...B 4 A... .... ..#. ...B Output: B A