#abc332d. D - Swapping Puzzle

D - Swapping Puzzle

Score : 425425 points

问题描述

你将得到两个网格 A 和 B,每个网格都有 HH 行和 WW 列。

对于满足条件 1iH1 \leq i \leq H1jW1 \leq j \leq W 的每一对整数 (i,j)(i, j),令 (i,j)(i, j) 表示第 ii 行和第 jj 列的单元格。在网格 A 中,单元格 (i,j)(i, j) 包含整数 Ai,jA_{i, j}。在网格 B 中,单元格 (i,j)(i, j) 包含整数 Bi,jB_{i, j}

你可以重复执行任意次数(包括零次)以下操作。在每次操作中,执行以下之一:

  • 选择一个满足 1iH11 \leq i \leq H-1 的整数 ii,并在网格 A 中交换第 ii 行和第 (i+1)(i+1) 行。
  • 选择一个满足 1iW11 \leq i \leq W-1 的整数 ii,并在网格 A 中交换第 ii 列和第 (i+1)(i+1) 列。

确定是否可以通过重复上述操作使网格 A 完全与网格 B 相同。如果可能,请输出完成此目标所需的最少操作次数。

这里,网格 A 等于网格 B 当且仅当,对于所有满足条件 1iH1 \leq i \leq H1jW1 \leq j \leq W 的整数对 (i,j)(i, j),网格 A 中单元格 (i,j)(i, j) 写有的整数等于网格 B 中单元格 (i,j)(i, j) 写有的整数。

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

Problem Statement

You are given two grids, A and B, each with HH rows and WW columns.

For each pair of integers (i,j)(i, j) satisfying 1iH1 \leq i \leq H and 1jW1 \leq j \leq W, let (i,j)(i, j) denote the cell in the ii-th row and jj-th column. In grid A, cell (i,j)(i, j) contains the integer Ai,jA_{i, j}. In grid B, cell (i,j)(i, j) contains the integer Bi,jB_{i, j}.

You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:

  • Choose an integer ii satisfying 1iH11 \leq i \leq H-1 and swap the ii-th and (i+1)(i+1)-th rows in grid A.
  • Choose an integer ii satisfying 1iW11 \leq i \leq W-1 and swap the ii-th and (i+1)(i+1)-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 (i,j)(i, j) satisfying 1iH1 \leq i \leq H and 1jW1 \leq j \leq W, the integer written in cell (i,j)(i, j) of grid A is equal to the integer written in cell (i,j)(i, j) of grid B.

Constraints

  • All input values are integers.
  • 2H,W52 \leq H, W \leq 5
  • 1Ai,j,Bi,j1091 \leq A_{i, j}, B_{i, j} \leq 10^9

Input

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

HH WW

A1,1A_{1, 1} A1,2A_{1, 2} \cdots A1,WA_{1, W}

A2,1A_{2, 1} A2,2A_{2, 2} \cdots A2,WA_{2, W}

\vdots

AH,1A_{H, 1} AH,2A_{H, 2} \cdots AH,WA_{H, W}

B1,1B_{1, 1} B1,2B_{1, 2} \cdots B1,WB_{1, W}

B2,1B_{2, 1} B2,2B_{2, 2} \cdots B2,WB_{2, W}

\vdots

BH,1B_{H, 1} BH,2B_{H, 2} \cdots BH,WB_{H, W}

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 33.

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