Chomp
Time limit1sMemory limit128 MB
Decide whether each 3-row Chomp position is winning and output a move to a losing position.
- Level
Medium6 of 10
- Topics
- Game theory, Dynamic programming
- Solved
- No attempts yet
Problem
Chomp is a game for two players, played on a rectangular chocolate bar cut into square cells. On a turn a player picks one remaining cell and eats it, together with every remaining cell whose column number is at least as large and whose row number is at least as large. The cell in the bottom left corner is poisoned, and the player who is forced to eat it loses.
Columns are numbered from the left and rows are numbered from the bottom, so the poisoned cell is the one in column , row .
A position is a winning position if some move leaves the opponent in a losing position. A position is a losing position if every move either eats the poisoned cell or leaves the opponent in a winning position. For example, the board and the L shapes whose two arms have equal length are losing positions, because the opponent can copy whatever the player to move does. The board, an L whose arms have different lengths, and a single row of cells are winning positions.
This problem asks you to solve Chomp on a board with rows and at most columns. Eating a cell always removes everything above it and to its right, so the cells left in a row are the leftmost ones, and any position that can arise is described by the number of cells left in the bottom row, the number left in the middle row and the number left in the top row, with .
Write a program that decides, for each given position, whether it is a winning or a losing position, and for a winning position finds a cell to eat next that leaves the opponent in a losing position.
Input
The first line contains the number of data sets . ()
Each of the next lines holds one data set. A line contains the data set number , the number of cells in the bottom row, the number of cells in the middle row and the number of cells in the top row, separated by single spaces, with . The data sets are independent.
Output
Print one line for each data set.
If the position is a losing position, print the data set number , a single space, and the capital letter L.
If the position is a winning position, print the data set number , the capital letter W, and then the column number and the row number of the cell to eat next, separated by single spaces. When several moves leave the opponent in a losing position, print the one with the smallest column number, and among those the one with the smallest row number.