In the exciting game of Join-
- Legal Position - - Illegal Position -
....... .......
....... .......
....... .......
....R.. Bad -> .......
...RB.. ..BR...
..BRB.. ...R...
.RBBR.. .RBBR..
In these pictures, each .
represents an empty slot, each R
represents a slot filled with a red piece, and each B
represents a slot filled with a blue piece. The left configuration is legal, but the right one is not. This is because one of the pieces in the third column (marked with the arrow) has not fallen down to the empty slot below it.
A player wins if they can place at least
- Four in a row -
R RRRR R R
R R R
R R R
R R R
In the "Legal Position" diagram at the beginning of the problem statement, both players had lined up two pieces in a row, but not three.
As it turns out, you are right now playing a very exciting game of Join-
- Start - - Rotate - - Gravity -
....... ....... .......
....... R...... .......
....... BB..... .......
...R... BRRR... R......
...RB.. RBB.... BB.....
..BRB.. ....... BRR....
.RBBR.. ....... RBBR...
Unfortunately, you only have time to rotate once before your opponent will notice.
All that remains is picking the right time to make your move. Given a board position, you should determine which player (or players!) will have
Notes
- You can rotate the board only once.
- Assume that gravity only takes effect after the board has been rotated completely.
- Only check for winners after gravity has finished taking effect.
Input Specification
The first line of the input gives the number of test cases,
The initial position in each test case will be a legal position that can occur during a game of Join-
Output Specification
For each test case, output one line containing Case #x: y
, where Red
, Blue
, Neither
, or Both
. Here,
Limits
Time limit: 30 seconds per test set.
Memory limit: 1 GB.
Small Dataset
Large Dataset
Sample Input
4
7 3
.......
.......
.......
...R...
...BB..
..BRB..
.RRBR..
6 4
......
......
.R...R
.R..BB
.R.RBR
RB.BBB
4 4
R...
BR..
BR..
BR..
3 3
B..
RB.
RB.
Sample Output
Case #1: Neither
Case #2: Both
Case #3: Red
Case #4: Blue
Comments