Hex Board State

Given each Hex board with red and blue stones, decide whether the position is unreachable, won by red, won by blue, or still open.

Medium5GraphBFSImplementationBrute forceNo attempts yetTime limit5sMemory limit512 MB

Problem

Hex is a board game that Piet Hein and John Nash designed independently of each other. This problem borrows the idea, but you do not need to have played Hex.

The game is played on an N×NN \times N board whose cells are hexagons. One player uses red stones and the other uses blue stones. The board starts empty, and the two players take turns placing one stone of their own color on a single cell of the board. A player may place a stone on any cell that no stone of either color occupies, and a stone does not have to go next to another stone of the same color. Which player moves first is chosen at random, with equal probability for the two players.

The upper and lower sides of the board are marked red, and the other two sides are marked blue. The goal is to link the two sides marked with your own color by a connected group of your own stones, and the first player to do that wins. Each of the four corner cells counts as touching both colors. The game ends the moment one player wins.

Write the position of a cell as row ii from the top and column jj from the left. The cells next to cell (i,j)(i, j) are those of (i1,j)(i-1, j), (i1,j+1)(i-1, j+1), (i,j1)(i, j-1), (i,j+1)(i, j+1), (i+1,j1)(i+1, j-1) and (i+1,j)(i+1, j) that lie on the board. Stones of the same color form one connected group when you can walk from one to the other through cells that are next to each other. Red wins by linking row i=1i = 1 to row i=Ni = N, and blue wins by linking column j=1j = 1 to column j=Nj = N.

Given a board position, report its status. The answer is one of these four.

  • Impossible: the two players could not have reached this position while following the rules.
  • Red wins: the player with the red stones has won.
  • Blue wins: the player with the blue stones has won.
  • Nobody wins: nobody has won yet. A game of Hex never ends in a draw, so the game is still open.

For a position that cannot be reached, the only correct answer is Impossible, even if red or blue already has a connected group linking the two sides marked with that color.

Input

The first line contains the number of test cases, TT. TT test cases follow. The first line of each test case contains the side length of the board, NN. The next NN lines each contain a string of length NN made up only of the characters B, R and .. B is a cell holding a blue stone, R is a cell holding a red stone, and . is an empty cell.

Limits

  • 1T1001 \le T \le 100
  • 1N101 \le N \le 10

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the status of the board: Impossible, Blue wins, Red wins or Nobody wins. The judge is case sensitive, so impossible, blue wins, red wins and nobody wins are wrong answers.