#P11357. [eJOI 2023] Square Grid Puzzle

[eJOI 2023] Square Grid Puzzle

Problem Description

There is an N×NN \times N matrix, whose elements are labeled from 0N210 \sim N^2 - 1. We want to make the element in row ii and column jj become iN+ji \cdot N + j using fewer than 3N3N 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 aa 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 bb 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 3N\geq 3N 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 NN.

The next NN lines each contain NN integers Fi,jF_{i,j}, representing the initial matrix.

Output Format

The first line contains an integer MM, representing the number of operations.

The next MM 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 A=3NA = 3N, B=2N2B = 2N^2, and let CC 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 M>BM > B, then you get 00 points for this test point.

Otherwise, if C<N2C < N^2, you get 50%CN250\% \cdot \frac{C}{N^2} of the score for this test point.

Otherwise, if AMBA \leq M \leq B, you get (40(BMBA)2+50)%(40 \cdot (\frac{B - M}{B - A})^2 + 50)\% of the score for this test point.

Otherwise, you get the full score.

Sample Explanation

In the first sample, 44 operations are used to restore the matrix, so you can get a score of 100%100\%.

In the second sample, 00 operations are used to restore 24=50%\frac{2}{4} = 50\% of the positions, so you can get a score of 25%25\% for this test point.

Constraints

For 100%100\% of the testdata, it is guaranteed that 2N92 \leq N \leq 9, and NN is uniformly distributed among the test points.

Translated by ChatGPT 5