#P5267. [NOI2014] 消除游戏

[NOI2014] 消除游戏

Problem Description

Recently, Little Z became addicted to a new elimination game. The game is played on an n×mn \times m grid. Initially, each cell contains an integer from 090 \sim 9. After eliminations, blank cells may appear in the grid, denoted by 1-1. For convenience, denote the number in row ii, column jj as Ai,jA_{i,j}, and its coordinate as (i,j)(i,j).

Given three parameters lmin,lmaxl_{\min}, l_{\max}, and KK, the player can perform at most KK operations. In each operation, the player needs to find a path of length ll in the grid. Formally, the path is represented by two sequences of length ll, x1,x2,,xlx_1, x_2, \ldots, x_l and y1,y2,,yly_1, y_2, \ldots, y_l, satisfying:

  1. 1xin,1yim1 \le x_i \le n, 1 \le y_i \le m for 1il1 \le i \le l, i.e., (xi,yi)(x_i,y_i) is a valid position in the grid.
  2. $\left|x_i-x_{i+1} \right|+ \left|y_i-y_{i+1} \right|=1$ for 1i<l1 \le i \lt l, i.e., (xi,yi)(x_i,y_i) and (xi+1,yi+1)(x_{i+1},y_{i+1}) are adjacent cells in the grid.
  3. xixjx_i \neq x_j or yiyjy_i \neq y_j for 1i<jl1 \le i \lt j \le l, i.e., the path cannot pass through any cell more than once.
  4. Axi,yi1A_{x_i,y_i} \neq -1 for 1il1 \le i \le l, i.e., the path cannot pass through blank cells.
  5. Ax1,y10A_{x_1,y_1} \neq 0, i.e., the path cannot start with the digit 00.
  6. lminllmaxl_{\min} \le l \le l_{\max}, i.e., the path length must be within the given range.

Concatenate the digits along the path into an integer NN, formally:

N=i=1lAxi,yi×10liN=\sum\limits_{i=1}^l A_{x_i,y_i}\times 10^{l-i}

The game provides two parameters c1,c2c_1, c_2 to compute the score for this operation:

  1. If NN is a prime number, you get the prime score lc1l^{c_1}; otherwise, the prime score is 11.
  2. If NN is a palindrome (i.e., viewing the decimal representation of NN as a string, its reverse is exactly the same as itself), you get the palindrome score lc2l^{c_2}; otherwise, the palindrome score is 11.
  3. If both the prime score and the palindrome score are 11, then the score of this operation is 00. Otherwise, the score of this operation is the sum of the prime score and the palindrome score.

After each operation, if the score of this operation equals 00, then you waste one operation and the grid does not change. If the score of this operation is greater than 00, then the numbers on the path are replaced with blanks, and the numbers above blanks fall vertically. Formally, perform the following steps:

  1. Set Axi,yi1A_{x_i,y_i}\leftarrow -1 for 1il1\le i\le l.
  2. Enumerate all cells. If there exists a cell (i,j)(i, j) such that ini \neq n, Ai,j1A_{i,j} \neq -1, and Ai+1,j=1A_{i+1,j} = -1, then perform Ai+1,jAi,j,Ai,j1A_{i+1,j} \leftarrow A_{i,j}, A_{i,j}\leftarrow -1. Repeat this operation until no such cell exists in the grid.

We also provide a parameter FF. After all operations are completed, the player’s final score SS is determined by FF: if F=0F=0, then the final score is the sum of the scores of all operations mm; if F=1F=1, then the final score is the sum of the scores of all operations mm divided by 2d2^d and floored, i.e.,

$$S = \begin{cases} m, & F=0\\\\ \left \lfloor \frac{m}{2^d} \right \rfloor, & F=1 \end{cases}$$

where dd is the number of non-blank cells in the final grid.

Little Z is so obsessed with this interesting game that she cannot stop. She wants to ask you for help: for the given input parameters, provide an operation plan for the game state. Of course, the higher the final score, the better.

Input Format

This is an output-only problem.

For each input file, line 11 contains 88 space-separated integers n,m,K,lmin,lmax,c1,c2,Fn, m, K, l_{\min}, l_{\max}, c_1, c_2, F, with the same meanings as in the statement.

Then follow nn lines, each containing mm integers, describing the grid AA. Numbers are separated by one space.

The input file will not contain extra blank lines, and there will be no extra spaces at the end of lines.

Output Format

For the given 1010 input files game1.in ~ game10.in, you need to submit your output files game1.out ~ game10.out respectively.

The first line of the output file is an integer M (0MK)M \ (0 \leq M \leq K), the number of operations you perform.

Then the output file should contain MM lines, each describing one operation. For each line, the first integer ll indicates the length of the chosen path for this operation. Then follow 2l2l numbers: x1,y1,x2,y2,,xl,ylx_1, y_1, x_2, y_2, \dots, x_l, y_l.

The output file should not contain extra spaces or blank lines. Multiple integers on the same line should be separated by one space.

3 3 100 2 3 1 1 0
2 1 1
2 3 3
4 7 1
4
2 2 2 3 2
2 3 1 3 2
2 2 1 3 1
3 1 3 2 3 3 3
1 3 100 2 3 1 1 1
2 1 1
1
2 1 2 1 3

Hint

Sample Explanation 1

The numbers eliminated in the 44 eliminations and their corresponding scores are: 3737, score 2+1=32+1=3; 4141, score 2+1=32+1=3; 2222, score 1+2=31+2=3; 131131, score 3+3=63+3=6. The total score is 1515. There may be a better plan.

Sample Explanation 2

This plan contains only one elimination operation. The eliminated number is 1111, and the score for this operation is 2+2=42+2=4. Since F=1F=1, the final score is the sum of operation scores 44 divided by 21=22^1 = 2 and floored, which is 22. If you choose the elimination path 211211, you will get the best possible score for this game state, which is 44.

Scoring

For each test, we set 99 scoring parameters a10,a9,,a2a_{10},a_9, \dots ,a_2. If your output is invalid, you get 00 points. Otherwise, if the game score in your solution is wuserw_{user}, your score will be given by the table below:

::cute-table{tuack=2}

Score Condition Score Condition
1010 wusera10w_{user}\ge a_{10} 55 a5wuser<a6a_5\le w_{user}\lt a_6
99 a9wuser<a10a_9\le w_{user}\lt a_{10} 44 a4wuser<a5a_4\le w_{user}\lt a_5
88 a8wuser<a9a_8\le w_{user}\lt a_9 33 a3wuser<a4a_3\le w_{user}\lt a_4
77 a7wuser<a8a_7\le w_{user}\lt a_8 22 a2wuser<a3a_2\le w_{user}\lt a_3
66 a6wuser<a7a_6\le w_{user}\lt a_7 11 0<wuser<a20\lt w_{user}\lt a_2

Additional Files

The checker must be compiled and used by yourself.

Please go to Github to download the latest source code of testlib.h.

Translated by ChatGPT 5