#P13390. [GCJ 2010 #1A] Rotate

[GCJ 2010 #1A] Rotate

题目描述

In the exciting game of Join-KK, red and blue pieces are dropped into an NN-by-NN table. The table stands up vertically so that pieces drop down to the bottom-most empty slots in their column. For example, consider the following two configurations:

    - Legal Position -

          .......
          .......
          .......
          ....R..
          ...RB..
          ..BRB..
          .RBBR..
   - Illegal Position -

          .......
          .......
          .......
          .......
   Bad -> ..BR...
          ...R...
          .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 KK pieces of their color in a row, either horizontally, vertically, or diagonally. The four possible orientations are shown below:

      - 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-KK, and you have a tricky plan to ensure victory! When your opponent is not looking, you are going to rotate the board 90 degrees clockwise onto its side. Gravity will then cause the pieces to fall down into a new position as shown below:

    - Start -

     .......
     .......
     .......
     ...R...
     ...RB..
     ..BRB..
     .RBBR..
   - Rotate -

     .......
     R......
     BB.....
     BRRR...
     RBB....
     .......
     .......
   - Gravity -

     .......
     .......
     .......
     R......
     BB.....
     BRR....
     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 KK pieces in a row after you rotate the board clockwise and gravity takes effect in the new direction.

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.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow, each beginning with a line containing the integers NN and KK. The next NN lines will each be exactly NN characters long, showing the initial position of the board, using the same format as the diagrams above.

The initial position in each test case will be a legal position that can occur during a game of Join-KK. In particular, neither player will have already formed KK pieces in a row.

输出格式

For each test case, output one line containing "Case #x: y", where xx is the case number (starting from 1), and yy is one of "Red", "Blue", "Neither", or "Both". Here, yy indicates which player or players will have KK pieces in a row after you rotate the board.

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.
Case #1: Neither
Case #2: Both
Case #3: Red
Case #4: Blue

提示

Limits

  • 1T1001 \leqslant T \leqslant 100.
  • 3KN3 \leqslant K \leqslant N.

Small dataset (11 Pts, Test set 1 - Visible)

  • 3N73 \leqslant N \leqslant 7.

Large dataset (12 Pts, Test set 2 - Hidden)

  • 3N503 \leqslant N \leqslant 50.