Under construction

Still being built. Things may change

Rotate the Board

Interview

Time limit5sMemory limit512 MB

Summary
Rotate the board 90 degrees clockwise, let every piece fall to the lowest empty cell, then report whether Red, Blue, both, or neither has K in a row.
Level

Easy3 of 10

Topics
Simulation, Matrix, Brute force
Solved
No attempts yet

Problem

In the game Join-K, red and blue pieces drop into an N×NN \times N board. The board stands upright, so a piece falls to the lowest empty cell of the column it is dropped into. In the pictures below, '.' is an empty cell, 'R' is a cell holding a red piece, and 'B' is a cell holding a blue piece.

This position is legal.

.......
.......
.......
....R..
...RB..
..BRB..
.RBBR..

This position is not.

.......
.......
.......
.......
..BR...
...R...
.RBBR..

The 'B' in the third column has an empty cell under it, so no sequence of drops produces that board.

A player wins by lining up at least KK pieces of their own color vertically, horizontally, or along either diagonal. The four orientations, drawn with K=4K = 4, are:

R...   RRRR   R...   ...R
R...   ....   .R..   ..R.
R...   ....   ..R.   .R..
R...   ....   ...R   R...

In the legal position above, both players have two pieces in a row and neither has three.

You are in the middle of a game of Join-K, and while your opponent looks away you are going to rotate the whole board 90 degrees clockwise. Gravity then pulls every piece down in the new orientation.

Start:

.......
.......
.......
...R...
...RB..
..BRB..
.RBBR..

Right after the clockwise rotation:

.......
R......
BB.....
BRRR...
RBB....
.......
.......

After gravity finishes:

.......
.......
.......
R......
BB.....
BRR....
RBBR...

You can rotate the board only once. Gravity acts only after the board has been rotated all the way, and you look for winners only after gravity has finished.

Given a starting position, report which player or players have KK pieces in a row once the board has been rotated clockwise and the pieces have settled.

Input

The first line contains the number of test cases TT. Each test case begins with a line holding two integers NN and KK. The next NN lines each hold exactly NN characters and describe the starting board in the format above.

Every starting board is a legal position that can occur in a game of Join-K, and neither player already has KK pieces in a row.

  • 1≤T≤1001 \le T \le 100
  • 3≤N≤73 \le N \le 7
  • 3≤K≤N3 \le K \le N

Output

For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y is one of Red, Blue, Neither, or Both. y names the player or players who have KK pieces in a row after the rotation and the fall.

Examples1

  1. Example 1

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