#abc332d. D - Swapping Puzzle
D - Swapping Puzzle
Score : points
问题描述
你将得到两个网格 A 和 B,每个网格都有 行和 列。
对于满足条件 和 的每一对整数 ,令 表示第 行和第 列的单元格。在网格 A 中,单元格 包含整数 。在网格 B 中,单元格 包含整数 。
你可以重复执行任意次数(包括零次)以下操作。在每次操作中,执行以下之一:
- 选择一个满足 的整数 ,并在网格 A 中交换第 行和第 行。
- 选择一个满足 的整数 ,并在网格 A 中交换第 列和第 列。
确定是否可以通过重复上述操作使网格 A 完全与网格 B 相同。如果可能,请输出完成此目标所需的最少操作次数。
这里,网格 A 等于网格 B 当且仅当,对于所有满足条件 和 的整数对 ,网格 A 中单元格 写有的整数等于网格 B 中单元格 写有的整数。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given two grids, A and B, each with rows and columns.
For each pair of integers satisfying and , let denote the cell in the -th row and -th column. In grid A, cell contains the integer . In grid B, cell contains the integer .
You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:
- Choose an integer satisfying and swap the -th and -th rows in grid A.
- Choose an integer satisfying and swap the -th and -th columns in grid A.
Determine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.
Here, grid A is identical to grid B if and only if, for all pairs of integers satisfying and , the integer written in cell of grid A is equal to the integer written in cell of grid B.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
If it is impossible to make grid A identical to grid B, output -1
. Otherwise, print the minimum number of operations required to make grid A identical to grid B.
Sample Input 1
4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19
Sample Output 1
3
Swapping the fourth and fifth columns of the initial grid A yields the following grid:
1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19
Then, swapping the second and third rows yields the following grid:
1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19
Finally, swapping the second and third columns yields the following grid, which is identical to grid B:
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19
You can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print .
Sample Input 2
2 2
1 1
1 1
1 1
1 1000000000
Sample Output 2
-1
There is no way to perform the operation to make grid A match grid B, so print -1
.
Sample Input 3
3 3
8 1 6
3 5 7
4 9 2
8 1 6
3 5 7
4 9 2
Sample Output 3
0
Grid A is already identical to grid B at the beginning.
Sample Input 4
5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029
Sample Output 4
20
update @ 2024/3/10 01:18:03