#P17900. [ICPC 2026 Nanchang I] Kingdam of Construction
[ICPC 2026 Nanchang I] Kingdam of Construction
Problem Description
You are managing the valve matrix of the “Kingdam of Construction”.
Given an matrix (both and are even), where all elements of are distinct positive integers between and .
You can perform the following two types of operations in any order:
- Choose row () and reverse it left-to-right, i.e. for all , swap and ;
- Choose column () and reverse it top-to-bottom, i.e. for all , swap and .
Determine whether it is possible to transform into the target matrix after a finite number of operations, where is filled in row-major order with , i.e. . If it is possible, construct an operation sequence with no more than operations (it is also allowed to perform no operation, i.e. an empty operation sequence). It can be proved that if an operation sequence exists, then there also exists one with no more than operations.
Input Format
The first line contains a positive integer (), representing the number of test cases. For each test case:
The first line contains two positive integers and (, ), representing the number of rows and columns of matrix .
The next lines each contain positive integers , representing the elements of matrix .
It is guaranteed that both and are even; it is also guaranteed that every integer from to appears exactly once in matrix .
Output Format
For each test case:
If it is possible to transform matrix into the target matrix in finitely many operations, print “Yes” on the first line.
Then print one non-negative integer on the next line, representing the number of operations. You must ensure that (for each test case; that is, there is no restriction on the sum of over all test cases).
Then print lines, each containing two integers and (, if , otherwise ), where means reversing row , and means reversing column .
If no valid sequence of operations exists, output only one line containing “No”.
You can output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as positive responses.
4
2 4
4 3 2 1
8 7 6 5
2 4
1 3 2 4
5 6 7 8
4 2
1 2
3 4
7 8
5 6
4 4
4 15 14 16
8 6 11 12
5 7 10 9
13 3 2 1
Yes
2
0 1
0 2
Yes
5
1 2
0 1
1 2
0 1
1 2
No
No
Hint
For the second sample test case, the operation process is as follows:
$$\def \arr #1#2#3#4#5#6#7#8{ \begin{bmatrix} #1 & #2 & #3 & #4\\ #5 & #6 & #7 & #8 \end{bmatrix} } \def \arrRow #1#2#3#4#5#6#7#8{ \begin{bmatrix} \mathbf{#1} & \mathbf{#2} & \mathbf{#3} & \mathbf{#4}\\ #5 & #6 & #7 & #8 \end{bmatrix} } \def \arrCol #1#2#3#4#5#6#7#8{ \begin{bmatrix} #1 & \mathbf{#2} & #3 & #4\\ #5 & \mathbf{#6} & #7 & #8 \end{bmatrix} } \def \toCol {\xrightarrow{\text{column, 2}}} \def \toRow {\xrightarrow{\text{row, 1}}} \begin{array}{cccccc} & \arr 13245678 & \toCol & \arrCol 16245378 & \toRow & \arrRow 42615378 \\ \toCol & \arrCol 43615278 & \toRow & \arrRow 16345278 & \toCol & \arrCol 12345678.\\ \end{array}$$For the third and fourth sample test cases, the target matrix is and $\begin{bmatrix}1&2&3&4\\5&6&7&8\\9&10&11&12\\13&14&15&16\end{bmatrix}$, respectively. It can be proved that both cases have no solution.