#P11357. [eJOI 2023] Square Grid Puzzle
[eJOI 2023] Square Grid Puzzle
Problem Description
There is an matrix, whose elements are labeled from . We want to make the element in row and column become using fewer than operations.
You can perform two kinds of operations on the matrix:
-
D a[0] a[1] ... a[N-1]: Rearrange the first row of the matrix, then cyclically shift each column upward by one. You must ensure that the given is a permutation of the first row of the matrix. -
R b[0] b[1] ... b[N-1]: Rearrange the first column of the matrix, then cyclically shift each row leftward by one. You must ensure that the given is a permutation of the first column of the matrix.
For example, if the current matrix is:
| R/C | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 2 | 4 | 6 |
| 1 | 8 | 1 | 5 |
| 2 | 7 | 3 | 0 |
If we perform D 2 6 4 on the original matrix, the matrix becomes:
| R/C | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 8 | 1 | 5 |
| 1 | 7 | 3 | 0 |
| 2 | 6 | 2 | 4 |
If we perform R 2 8 7 on the original matrix, the matrix becomes:
| R/C | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 4 | 6 | 2 |
| 1 | 5 | 8 | |
| 2 | 3 | 0 | 7 |
Even if you use operations, or some numbers are not restored, you can still get partial score. See the Scoring section.
Input Format
The first line contains an integer .
The next lines each contain integers , representing the initial matrix.
Output Format
The first line contains an integer , representing the number of operations.
The next lines each describe one operation, in the format given above.
3
1 4 2
3 7 5
6 8 0
4
R 3 6 1
D 2 3 4
D 5 6 7
R 2 5 8
2
2 1
0 3
0
Hint
Scoring
Let , , and let be the number of positions that satisfy the requirement after the operations.
If your program does not terminate normally, or the output format is wrong, or , then you get points for this test point.
Otherwise, if , you get of the score for this test point.
Otherwise, if , you get of the score for this test point.
Otherwise, you get the full score.
Sample Explanation
In the first sample, operations are used to restore the matrix, so you can get a score of .
In the second sample, operations are used to restore of the positions, so you can get a score of for this test point.
Constraints
For of the testdata, it is guaranteed that , and is uniformly distributed among the test points.
Translated by ChatGPT 5