#P1312. [NOIP 2011 提高组] Mayan 游戏
[NOIP 2011 提高组] Mayan 游戏
Description
Mayan puzzle is a recently popular game. The game interface is a -row -column board stacked with some blocks. Blocks cannot be suspended in the air, i.e., a block must be placed on the bottom row or on top of another block. To clear the game means to eliminate all blocks within a given number of moves. The elimination rules are as follows:
- In each move, you may and only may drag one block horizontally (left or right) by one cell: When dragging this block, if the target position also has a block, the two blocks will swap positions (see Figures to in the sample I/O explanation). If the target position is empty, the dragged block will be pulled out of its original column and will fall from the target position (until it is supported; see Figures and below).

- At any time, if there are three or more consecutive blocks of the same color in any row or column, they will be eliminated immediately (see Figures 1 to 3).

Notes: a) If multiple groups of blocks simultaneously satisfy the elimination condition, all such groups will be eliminated at the same time (for example, in Figure , three blocks of color and three blocks of color are eliminated simultaneously, leaving one block of color ). b) When both a row and a column satisfy the elimination condition and they share a block, all blocks in that row and column that satisfy the condition will be eliminated simultaneously (for example, in the situation shown in Figure 5, blocks are eliminated at the same time).
- After blocks are eliminated, the blocks above the eliminated positions will fall, and the falling may trigger new eliminations. Note: No elimination occurs during the falling process.
Figures to above show the changes on the board after moving a block. The coordinates of the block in the lower-left corner of the board are . After moving the block at to the left, the interface changes from Figure to the state shown in Figure . At this time there are three consecutive blocks of color in one column, satisfying the elimination condition. After eliminating the three consecutive blocks of color , the blocks of color above fall, forming the situation shown in Figure .
Input Format
A total of lines.
- The first line is a positive integer , the number of moves required to clear the game.
- The next lines describe the game interface. Each line contains several integers separated by a space and ends with a . For each column (from left to right), the integers list the color numbers of the blocks from bottom to top. There are at most colors, numbered in order starting from , and the same number indicates the same color.
- The testdata guarantees that there are no eliminable blocks on the initial board.
Output Format
If there is a solution, output lines, each containing integers separated by a space, representing one move. Here is the coordinate of the block to move, and is the direction: means move to the right, means move to the left. Note: If there are multiple solutions, use as the primary key, as the secondary key, and prefer over , and output the lexicographically smallest solution. The coordinate of the lower-left corner of the board is .
If there is no solution, output a single line -1.
3
1 0
2 1 0
2 3 4 0
3 1 0
2 4 3 4 0
2 1 1
3 1 1
3 0 1
Hint
【Sample I/O Explanation】
In the direction of the arrows, these are Figures to in order.

The game state of the sample input is shown in the first image above. The three moves in order are: move the block at to the right, move the block at to the right, and move the block at to the right. In the end, all blocks on the board can be eliminated.
【Constraints】
- For of the testdata, all blocks on the initial board are on the bottom row.
- For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号