Bear and Cat Trap

All submissions for this problem are available.

This is an interactive problem.
It means that every time you output something, you must finish with a newline character and flush the output.
For example, in C++ use the fflush(stdout) function, in Java — call System.out.flush(), in Python — sys.stdout.flush(), and in Pascal — flush(output).
Only after flushing the output will you be able to read from the input again.

Limak is a little polar bear.
One of his favorite games is the Cat Trap (also known as Black Cat, Chat Noir or Circle The Cat).
The goal of the game is to prevent a cat from running away.
Limak loves it because cats are usually afraid of him (for no reason) and don't want to play with him.
You can try that game yourself: http://www.gamedesign.jp/flash/chatnoir/chatnoir.html.

In this problem, your task is to win in a modified version of the game.
The differences are:

The grid is a regular hexagon of side N = 20.

Initially no cells are blocked.

You must win in M turns (i.e. you can block at most M cells).

The cat follows some particular strategy, described below.

Rows are numbered -(N-1) through N-1 from top to bottom, so the row 0 is in the middle.
Columns are numbered -(N-1) through N-1 from left to right, where the column 0 is the diagonal that contains the first cell of the top row and the last cell of the bottom row.
For example, for N = 3 the grid would look like this (blue arrows are pointing at the column 0):

Let (r, c) denote the cell at the intersection of the r-th row and the c-th column.
The description above implies that the cell (r,c) has at most six adjacent cells: (r,c+1), (r-1,c+1), (r-1,c), (r,c-1), (r+1,c-1), (r+1,c).

A border cell is a cell with fewer than six adjacent cells (there are exactly 6*(N-1) border cells).
For any not-blocked cell X we define the distance to the border as the minimum number of moves the cat would need to reach a border cell, assuming that he starts from X and no more cells will be blocked.
In particular, the distance to the border is 0 for all not-blocked border cells.
The distance to the border is infinite if a cell is blocked or the cat can't reach any border cell.

The cat starts in the cell (0, 0), i.e. in the middle of the grid.
In each turn of the game:

The cat moves first.
If he is in a border cell, he runs away and you lose.
If he can't reach any border cell, you win (i.e. when the cat is in a connected component bounded by blocked cells).
Otherwise, the cat moves to one of the adjacent cells with the minimum distance to the border.
For example, if three adjacent cells are blocked, two cells have the distance to the border equal to 15, and one cell has the distance to the border equal to 17, then the cat chooses one of the two adjacent cells with the distance 15.
Also, in the very first move the cat chooses any of six adjacent cells, because all of them have the same distance to the border.

Then you can block one cell.
You can't block a cell if it's already blocked or it contains the cat.

Interaction

The first line of the input contains two integers T and M denoting the number of test cases and the maximum allowed number of times you can block a cell (per test case).
At the beginning of each test case, the cat starts in the cell (0, 0) and no cells are blocked.

When the cat is supposed to move, you should read from the input, a line that is either "WIN" or "CAT r c" (both without the quotes).
A line "WIN" means that the cat can't reach the border anymore and you should proceed to the next test case (and again read the first move of the cat), unless it's the last test case — then you should terminate your program.
A line "CAT r c" means that the cat moves to the cell (r, c).

After the cat moves, you should print a line in the format "BLOCK r c" denoting blocking the cell (r, c).
Remember about printing a newline character and flushing the output.
You can't skip a move and you can't block a cell if it's already blocked or it contains the cat.

Subtasks

Subtask #1 (35 points) 1 ≤ T ≤ 10, M = 500

Subtask #2 (65 points) 1 ≤ T ≤ 100, M = 20

Visualizer

You need Python3 to run the visualizer: link.
The instructions and the example usage are provided at the bottom of the file.

Example

your program

the system

2 500

CAT 1 -1

BLOCK -1 0

CAT 1 0

BLOCK -1 1

CAT 0 0

BLOCK 1 -1

CAT 0 1

BLOCK 0 -1

CAT 1 0

BLOCK 1 1

CAT 0 0

BLOCK 0 2

CAT 0 1

BLOCK -1 2

CAT 0 0

BLOCK 1 0

WIN

CAT 1 0

BLOCK 5 -12

...

...

WIN

Explanation

In the provided example, there are T = 2 test cases and in each of them you are allowed to block M = 500 cells.
Note that the example communication is invalid because the cat doesn't always move to an adjacent cell with the minimum distance to the border.
Such a situation won't happen in real tests.
And the second test case was given only to show you how the communication looks like if there are multiple test cases.

You can run the visualizer to see the simulation of the first test case.
For your convenience, the grid below shows the final situation, assuming that the size of the grid is N = 4 (but remember that N = 20 in all tests).

Comments

Please login at the top to post a comment.

SUCCESSFUL SUBMISSIONS

Fetching successful submissions

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.

Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.

Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.

Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.

Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to INR 20,000 (for Indian Community), $700 (for Global Community) and lots more CodeChef goodies up for grabs.