#abc336f. F - Rotation Puzzle
F - Rotation Puzzle
Score: points
问题描述
存在一个具有 行和 列的网格。最初,从 到 的每个整数恰好在网格中各写一次。
具体来说,对于 和 ,第 行(从上到下数)第 列(从左到右数)的格子包含整数 。以下,记 为第 行(从上到下数)和第 列(从左到右数)的格子。
确定是否有可能通过最多重复执行以下操作 至多20次(可能为零次)达到一种状态:对于所有整数对 (满足 ),格子 中包含整数 。
如果可能,请输出所需的最小操作次数。如果在20次或更少的操作内不可能实现(包括无论重复多少次都无法完成的情况),则输出 。
操作:从网格中选择一个尺寸为 的矩形,并将其旋转 度。 更确切地说,选择整数 和 (满足 ), 并对于所有满足 和 的整数对 , 同时将格子 写有的整数替换为格子 写有的数字。
注意,只需满足格子中所写的整数满足条件即可;它们的书写方向无关紧要。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a grid with rows and columns. Initially, each integer from to is written exactly once in the grid.
Specifically, for and , the cell at the -th row from the top and the -th column from the left contains .
Below, let denote the cell at the -th row from the top and the -th column from the left.
Determine if it is possible to reach a state where, for all pairs of integers ,
the cell contains the integer by repeating the following operation at most times (possibly zero).
If it is possible, print the minimum number of operations required.
If it is impossible in or fewer operations (including the case where it is impossible no matter how many times the operation is repeated), print .
Operation: Choose a rectangle of size from the grid and rotate it degrees.
More precisely, choose integers and ,
and for all pairs of integers satisfying and ,
simultaneously replace the integer written in cell with the number written in cell .
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
- If , then
- All input values are integers.
Input
The input is given from Standard Input in the following format:
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 or fewer operations, output .
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 operations.
- Operate by choosing the rectangle in the top-left corner. That is, by choosing and .
- Operate by choosing the rectangle in the bottom-right corner. That is, by choosing and .
On the other hand, it is impossible to satisfy the conditions with one or fewer operations, so print .
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 or fewer operations, so print .
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