#P1312. Mayan Puzzle Simulation

    ID: 14599 Type: Default 1000ms 256MiB

Mayan Puzzle Simulation

Mayan Puzzle Simulation

The Mayan Puzzle is a popular game played on a $7\times5$ board. Blocks of various colors are stacked on the board. A block must always be supported; that is, it either lies on the bottom row or directly on top of another block.

The goal of the game is to eliminate all the blocks within a given number of moves. Elimination happens according to the following rules:

  1. Move: In each move you may choose exactly one block and drag it horizontally by one cell (i.e. left or right). Let the block being moved be at coordinate $(x,y)$ in the board (with the bottom-left cell as $(0,0)$) and the target cell be at $(x\pm1, y)$.
    • If the target cell contains a block, then the two blocks swap positions. (See Figures 6 and 7 in the sample explanation.)
    • If the target cell is empty, then the block is removed from its original column and moved to the target column. It will then fall down until it is supported by either the bottom of the board or another block. (See Figures 1 and 2.)
  2. Elimination: At any moment, if there exists a horizontal row or a vertical column where three or more consecutive blocks of the same color appear, then all those blocks are eliminated simultaneously.
    • If multiple such groups exist, all are removed at the same time. (For example, see Figure 4.)
    • If a block is part of both a row and a column group, then it is eliminated once along with all other blocks in both groups. (See Figure 5.)
  3. Falling: After elimination, any block that has an empty cell beneath it will fall down until it becomes supported. Note that during the falling process, further eliminations do not occur until the fall is complete.

Game Objective: You are given the initial configuration of the board, the maximum number of moves allowed, and a sequence of moves. The moves are executed one by one (up to the allowed move count). If at any time (including before any move is made) the board becomes empty, the game is considered cleared. Your task is to simulate the game and output Yes if all blocks are eliminated, or No otherwise.

Input Coordinates: The board columns are indexed from 0 to 4 and rows from 0 to 6 (with row 0 being the bottom).

inputFormat

The input is given in the following format:

M
r0
r1
r2
r3
r4
r5
r6
N
move1
move2
... 

Where:

  • M is an integer representing the maximum number of moves allowed.
  • r0 to r6 each represent one row of the board (from bottom to top). Each row contains 5 integers separated by spaces. A value of 0 indicates an empty cell, and a positive integer represents a block's color. In a valid board, blocks in any column are stacked from the bottom up (i.e. no gaps between blocks).
  • N is the number of moves provided.
  • Each move is given in one line containing two integers x y and a character d separated by spaces. Here, (x,y) is the coordinate of the block to move, and d is either L (move left) or R (move right). It is guaranteed that the move will be within board boundaries.

outputFormat

Output a single line with either Yes if the board becomes empty (all blocks eliminated) at any point during the simulation, or No otherwise.

sample

1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0
Yes