#P5267. Elimination Game
Elimination Game
Elimination Game
In the Elimination Game, you are given an n × m grid filled with digits from 0 to 9. Each cell is labeled as \(A_{i,j}\) with coordinates \((i,j)\). Some cells might become empty (denoted by -1) after operations. You can perform at most \(K\) operations. In each operation, you need to choose a path of length \(l\) (where \(l_{\min} \le l \le l_{\max}\)) that satisfies the following conditions:
- Each position \((x_i,y_i)\) along the path is within the grid: \(1 \le x_i \le n\) and \(1 \le y_i \le m\).
- Each two consecutive positions are adjacent: \(|x_i-x_{i+1}| + |y_i-y_{i+1}| = 1\) for \(1 \le i < l\).
- The path does not contain any repeated cells (i.e. for any \(1 \le i < j \le l\), either \(x_i \neq x_j\) or \(y_i \neq y_j\)).
- Every cell on the path must not be empty: \(A_{x_i,y_i} \neq -1\) for \(1 \le i \le l\).
- The starting cell must not have the digit 0: \(A_{x_1,y_1} \neq 0\).
- The path length satisfies \(l_{\min} \le l \le l_{\max}\).
The digits on the path form an integer \(N\) defined as:
[ N = \sum_{i=1}^{l} A_{x_i,y_i} \times 10^{l-i} ]
For each operation, two parameters \(c_1\) and \(c_2\) determine your score:
- If \(N\) is a prime number, you earn a prime score of \(l^{c_1}\); otherwise, the prime score is 1.
- If \(N\) is a palindrome (its decimal representation reads the same forwards and backwards), you earn a palindrome score of \(l^{c_2}\); otherwise, the palindrome score is 1.
- If both scores are 1, the operation score becomes 0 (which still counts as an operation without any board change); otherwise, the operation score is the sum of the two scores.
After an operation with a positive score, the numbers along the chosen path are removed (set to -1) and the remaining numbers fall vertically downwards to fill the empty spaces. Formally:
- For every cell in the path, set \(A_{x_i,y_i} \leftarrow -1\) for \(1 \le i \le l\).
- Then, repeatedly for any cell \((i,j)\) (with \(i \neq n\)) such that \(A_{i,j} \neq -1\) and \(A_{i+1,j} = -1\), perform a swap: \(A_{i+1,j} \leftarrow A_{i,j}\) and \(A_{i,j} \leftarrow -1\), until no more moves are possible.
After all operations, a parameter \(F\) determines the final score \(S\):
[ S = \begin{cases} m, & F = 0 \ \left\lfloor \frac{m}{2^d} \right\rfloor, & F = 1 \end{cases} ]
where \(m\) is the total sum of operation scores and \(d\) is the number of non-empty cells in the final grid. Your task is to provide an operation plan (i.e. a sequence of moves) for the given game state; the higher the final score \(S\), the better. Note that if an operation yields a score of 0, the board remains unchanged but counts towards your \(K\) operations.
inputFormat
The input begins with a line containing two integers \(n\) and \(m\) (the number of rows and columns of the grid).
The second line contains six integers: \(l_{\min}\), \(l_{\max}\), \(K\), \(c_1\), \(c_2\), and \(F\).
Then follow \(n\) lines, each containing \(m\) integers, representing the grid. Each grid cell contains an integer between 0 and 9.
You must output an operation plan that does not exceed \(K\) operations.
outputFormat
Output your operation plan as follows:
The first line contains a single integer \(t\) (\(0 \le t \le K\)), the number of operations you choose to perform.
Then for each operation, output a line starting with an integer \(l\) (the length of the selected path) followed by \(l\) pairs of integers \(x_i\) and \(y_i\) denoting the coordinates of the path in order.
If you decide not to perform any operations, simply output 0.
sample
1 1
1 1 0 1 1 0
5
0