33OJ
登录 注册

#P17900. [ICPC 2026 Nanchang I] Kingdam of Construction

    ID: 20636 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>Special Judge江西构造ICPC2026Ad-hoc省赛/邀请赛

[ICPC 2026 Nanchang I] Kingdam of Construction

Problem Description

You are managing the valve matrix of the “Kingdam of Construction”.

Given an n×mn\times m matrix aa (both nn and mm are even), where all elements of aa are distinct positive integers between 11 and n×mn\times m.

You can perform the following two types of operations in any order:

  • Choose row ii (1≤i≤n1\le i\le n) and reverse it left-to-right, i.e. for all 1≤j≤m21\le j\le \frac m2, swap ai,ja_{i,j} and ai,m−j+1a_{i,m-j+1};
  • Choose column jj (1≤j≤m1\le j\le m) and reverse it top-to-bottom, i.e. for all 1≤i≤n21\le i\le \frac n2, swap ai,ja_{i,j} and an−i+1,ja_{n-i+1,j}.

Determine whether it is possible to transform aa into the n×mn\times m target matrix bb after a finite number of operations, where bb is filled in row-major order with 1,2,…,n×m1,2,\ldots,n\times m, i.e. bi,j=(i−1)×m+jb_{i,j}=(i-1)\times m+j. If it is possible, construct an operation sequence with no more than 99619961 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 99619961 operations.

Input Format

The first line contains a positive integer tt (1≤t≤251\le t\le 25), representing the number of test cases. For each test case:

The first line contains two positive integers nn and mm (2≤n≤1342\le n\le 134, 2≤m≤1342\le m\le 134), representing the number of rows and columns of matrix aa.

The next nn lines each contain mm positive integers ai,1,ai,2,…,ai,ma_{i,1},a_{i,2},\ldots,a_{i,m}, representing the elements of matrix aa.

It is guaranteed that both nn and mm are even; it is also guaranteed that every integer from 11 to n×mn\times m appears exactly once in matrix aa.

Output Format

For each test case:

If it is possible to transform matrix aa into the target matrix bb in finitely many operations, print “Yes” on the first line.

Then print one non-negative integer cntcnt on the next line, representing the number of operations. You must ensure that 0≤cnt≤99610\le cnt\le 9961 (for each test case; that is, there is no restriction on the sum of cntcnt over all test cases).

Then print cntcnt lines, each containing two integers opop and idxidx (op∈{0,1}op\in\{0,1\}, 1≤idx≤n1\le idx\le n if op=0op=0, otherwise 1≤idx≤m1\le idx\le m), where op=0op=0 means reversing row idxidx, and op=1op=1 means reversing column idxidx.

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 bb is [12345678]\begin{bmatrix}1&2\\3&4\\5&6\\7&8\end{bmatrix} 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.