#abc336f. F - Rotation Puzzle

F - Rotation Puzzle

Score: 550550 points

问题描述

存在一个具有 HH 行和 WW 列的网格。最初,从 11(H×W)(H \times W) 的每个整数恰好在网格中各写一次。

具体来说,对于 1iH1 \leq i \leq H1jW1 \leq j \leq W,第 ii 行(从上到下数)第 jj 列(从左到右数)的格子包含整数 Si,jS_{i,j}。以下,记 (i,j)(i,j) 为第 ii 行(从上到下数)和第 jj 列(从左到右数)的格子。

确定是否有可能通过最多重复执行以下操作 至多20次(可能为零次)达到一种状态:对于所有整数对 (i,j)(i,j) (满足 1iH,1jW1 \leq i \leq H, 1 \leq j \leq W),格子 (i,j)(i,j) 中包含整数 ((i1)×W+j)((i-1) \times W + j)

如果可能,请输出所需的最小操作次数。如果在20次或更少的操作内不可能实现(包括无论重复多少次都无法完成的情况),则输出 1-1

操作:从网格中选择一个尺寸为 (H1)×(W1)(H-1) \times (W-1) 的矩形,并将其旋转 180180 度。 更确切地说,选择整数 xxyy (满足 0x,y10 \leq x, y \leq 1), 并对于所有满足 1iH11 \leq i \leq H-11jW11 \leq j \leq W-1 的整数对 (i,j)(i,j), 同时将格子 (i+x,j+y)(i+x,j+y) 写有的整数替换为格子 (Hi+x,Wj+y)(H-i+x,W-j+y) 写有的数字。

注意,只需满足格子中所写的整数满足条件即可;它们的书写方向无关紧要。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a grid with HH rows and WW columns. Initially, each integer from 11 to (H×W)(H \times W) is written exactly once in the grid.
Specifically, for 1iH1 \leq i \leq H and 1jW1 \leq j \leq W, the cell at the ii-th row from the top and the jj-th column from the left contains Si,jS_{i,j}.
Below, let (i,j)(i,j) denote the cell at the ii-th row from the top and the jj-th column from the left.

Determine if it is possible to reach a state where, for all pairs of integers (i,j)(i,j) (1iH,1jW)(1 \leq i \leq H, 1 \leq j \leq W),
the cell (i,j)(i,j) contains the integer ((i1)×W+j)((i-1) \times W + j) by repeating the following operation at most 2020 times (possibly zero).
If it is possible, print the minimum number of operations required.
If it is impossible in 2020 or fewer operations (including the case where it is impossible no matter how many times the operation is repeated), print 1-1.

Operation: Choose a rectangle of size (H1)×(W1)(H-1) \times (W-1) from the grid and rotate it 180180 degrees.
More precisely, choose integers xx and yy (0x,y1)(0 \leq x, y \leq 1),
and for all pairs of integers (i,j)(i,j) satisfying 1iH11 \leq i \leq H-1 and 1jW11 \leq j \leq W-1,
simultaneously replace the integer written in cell (i+x,j+y)(i+x,j+y) with the number written in cell (Hi+x,Wj+y)(H-i+x,W-j+y).

Note that it is only necessary for the integers written in the cells to satisfy the condition; the orientation in which they are written does not matter.

Constraints

  • 3H,W83 \leq H,W \leq 8
  • 1Si,jH×W1 \leq S_{i,j} \leq H \times W
  • If (i,j)(i,j)(i,j) \neq (i',j'), then Si,jSi,jS_{i,j} \neq S_{i',j'}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

HH WW

S1,1S_{1,1} S1,2S_{1,2} \ldots S1,WS_{1,W}

S2,1S_{2,1} S2,2S_{2,2} \ldots S2,WS_{2,W}

\vdots

SH,1S_{H,1} SH,2S_{H,2} \ldots SH,WS_{H,W}

Output

Print the minimum number of operations required to meet the conditions of the problem statement.
If it is impossible to satisfy the conditions with 2020 or fewer operations, output 1-1.

Sample Input 1

3 3
9 4 3
2 1 8
7 6 5

Sample Output 1

2

Operating in the following order will satisfy the conditions of the problem statement in 22 operations.

  • Operate by choosing the rectangle in the top-left corner. That is, by choosing x=0x=0 and y=0y=0.
  • Operate by choosing the rectangle in the bottom-right corner. That is, by choosing x=1x=1 and y=1y=1.

On the other hand, it is impossible to satisfy the conditions with one or fewer operations, so print 22.

Sample Input 2

4 6
15 18 1 14 3 4
23 24 19 8 9 12
13 2 17 6 5 16
21 22 7 20 11 10

Sample Output 2

-1

It is impossible to satisfy the conditions with 2020 or fewer operations, so print 1-1.

Sample Input 3

4 6
1 4 13 16 15 18
21 20 9 12 23 10
17 14 5 6 3 2
11 22 7 24 19 8

Sample Output 3

20

Sample Input 4

4 3
1 2 3
4 5 6
7 8 9
10 11 12

Sample Output 4

0

update @ 2024/3/10 01:26:09