Renju
Problem
Two players play a game of Omok on a board.
The board consists of cells, ranging from row and column to row and column . A cell at the -th row and -th column is denoted as .
Each cell can be in one of three states: an empty cell ., the first player's stone B, or the second player's stone W. Initially, the stones are arranged as follows:
| State | Count | Initial Placement |
|---|---|---|
Empty cell . |
Entire board from to | |
First-player stone B |
None | |
Second-player stone W |
None |
The game begins with the first player, and the two players take turns.
- During their turn, a player can select an empty cell to place their stone, or select
PASS. - On the first turn, the first player cannot select
PASSand must place a stone at . - On the second turn, the second player cannot select
PASSand must place a stone at where . - On the third turn, the first player cannot select
PASSand must place a stone at where .

The figure above is an example of the first player placing a stone at .
1. TERMS AND DEFINITIONS
The key terms that make up the Renju rules are defined as follows:

- ROW: A combination of empty cells and stones placed consecutively in a diagonal, vertical, or horizontal line. A ROW must contain at least one stone, and all stones within the ROW must be either all
Bor allW. Both ends of the ROW must be blocked by the edge of the board, a stone of a different color, or an empty cell.
- A and B are ROWs.
- C is not a ROW because one of its ends is a stone of the same color.
- UNBROKEN ROW: A ROW with no empty cells inside.
- B is not an UNBROKEN ROW because there is an empty cell inside.
- D is an UNBROKEN ROW.
- FIVE IN A ROW: An UNBROKEN ROW consisting of stones.
- E is a FIVE IN A ROW.
- OVERLINE: An UNBROKEN ROW consisting of or more stones.
- F is an OVERLINE consisting of stones.

- FOUR: A ROW consisting of stones to which one more stone can be added to create a FIVE IN A ROW.
- G and H are FOURs because one more stone can be added to create a FIVE IN A ROW.
- STRAIGHT FOUR: An UNBROKEN ROW consisting of stones where there are ways to add one more stone to create a FIVE IN A ROW.
- H is a STRAIGHT FOUR because there are ways to add one more stone to create a FIVE IN A ROW.
- G is not a STRAIGHT FOUR because there is only way to add one more stone to create a FIVE IN A ROW.

- THREE: A ROW consisting of stones where adding one more stone does not create a FIVE IN A ROW but can create a STRAIGHT FOUR.
- I and J are THREEs because adding one more stone does not create a FIVE IN A ROW while it can create a STRAIGHT FOUR.
- K, L, and M are not THREEs because adding one more stone cannot create a STRAIGHT FOUR.
- N is not a THREE because adding one more stone can create a STRAIGHT FOUR, but it simultaneously creates a FIVE IN A ROW.

- DOUBLE FOUR: Placing a stone in an empty cell such that or more different FOURs containing that cell are created.
- O is a DOUBLE FOUR because or more different FOURs are created.
- DOUBLE THREE: Placing a stone in an empty cell such that or more different THREEs containing that cell are created.
- P is not a DOUBLE THREE because only one THREE is created.
- Q is a DOUBLE THREE because or more different THREEs are created.
2. FORBIDDEN MOVES
To reduce the advantage of the first player over the second player, the Renju rules apply the following additional rules to the first player.

- The first player cannot create an OVERLINE, DOUBLE FOUR, or DOUBLE THREE unless it simultaneously creates a FIVE IN A ROW.
- The first player cannot play at A because it is a DOUBLE FOUR.
- The first player cannot play at B because it is an OVERLINE.
- The first player can play at C because although it is a DOUBLE FOUR, it simultaneously creates a FIVE IN A ROW.
- The first player cannot play at D and E because they are DOUBLE THREEs.
- The first player cannot play at F, H, and I because they are DOUBLE THREEs.
- The first player cannot play at G because it is a DOUBLE FOUR.

- However, Black's DOUBLE THREE is allowed when, among the THREEs created according to Article 9.3 of the Renju rules, the cell where a stone must be added to make a STRAIGHT FOUR becomes an OVERLINE or DOUBLE FOUR, resulting in or fewer valid THREEs. It is also allowed if the cell where a stone must be added to make a STRAIGHT FOUR becomes another forbidden DOUBLE THREE, resulting in or fewer valid THREEs. To check whether another DOUBLE THREE is allowed, you must first evaluate it based on the preceding criteria, and then continue simulating the creation of STRAIGHT FOURs to determine any cascading DOUBLE THREEs in the same manner.
- The first player can play at J because the cell where a stone must be added to make the horizontally created THREE into a STRAIGHT FOUR becomes an OVERLINE, leaving only valid THREE.
- The first player can play at K because the cell where a stone must be added to make the diagonally (bottom-right) created THREE into a STRAIGHT FOUR becomes a DOUBLE FOUR, leaving only valid THREE.
- The first player can play at L because the cell where a stone must be added to make the horizontally created THREE into a STRAIGHT FOUR becomes an unallowed DOUBLE THREE, leaving only valid THREE.
3. WIN OF A GAME
The game ends when any of the following conditions are met:
- If the first player creates a FIVE IN A ROW, the first player wins.
- If the second player creates a FIVE IN A ROW or an OVERLINE, the second player wins.
- If both players consecutively select
PASS, the game ends in a draw.
Please design an AI to become the ultimate winner of the board!
NOTE.
The Renju rules additionally apply the opening rules of Articles 12.1~12.9 to further reduce the advantage of the first player over the second player, but for the convenience of implementation in this problem, the opening rules have been intentionally omitted.
Input
The judge program communicates with the agent using single-line text commands as follows:
| Command | Judge → Agent (Input) | Agent → Judge (Output) | Time Limit (ms) | Description |
|---|---|---|---|---|
| READY | READY (FIRST or SECOND) |
OK |
3000 |
Informs whether you are the first or second player. |
| TURN | TURN my_time opp_time |
MOVE x y |
my_time |
Informs your remaining time and the opponent's remaining time. Output the chosen . For PASS, output MOVE -1 -1. |
| OPP | OPP x y time |
- | - | Informs the opponent's last move and the time they used. If the opponent chose PASS, OPP -1 -1 time is given. |
| FINISH | FINISH |
- | - | Signals the end of the game. The agent must terminate normally without further output. |
x,yare integers.my_time,opp_time, andtimeare integers representing time in milliseconds.- After every output, you must print a newline character and flush the buffer.
- Failure to output within the time limit results in a Time Limit Exceeded (
TLE) verdict. - Outputting an invalid format or an illegal move during a TURN command results in a Runtime Error (
RE) verdict.
Example
| First Player Input | First Player Output | Second Player Input | Second Player Output |
|---|---|---|---|
READY FIRST |
READY SECOND |
||
OK |
OK |
||
TURN 10000 10000 |
|||
MOVE 8 8 |
|||
OPP 8 8 50 |
|||
TURN 10000 9950 |
|||
MOVE 9 8 |
|||
OPP 9 8 20 |
|||
| ... | ... | ... | ... |
FINISH |
FINISH |
Sample Code
- C20 : sample.c
- CPP20 : sample.cpp
- PYTHON3, PYPY3 : sample.py
Limit
- Time: In the first TURN command,
my_timeis for each player. - Memory:
- Judge environment: HELP > Judge environment
