QOJ.ac

QOJ

حد الوقت: 3.0 s حد الذاكرة: 1024 MB مجموع النقاط: 100 قابلة للهجوم ✓

#10494. 方块拼图

الإحصائيات

考虑一个 $3 \times 3$ 的网格。每个单元格中都有一个整数。从 $1$ 到 $9$ 的每个整数(包含 $1$ 和 $9$)在网格中恰好出现一次。

你可以对网格执行以下三种类型的操作:

  • 向右平移一行:选择一行,将其最右侧的元素移动到最左侧的位置。
  • 向下平移一列:选择一列,将其最底部的元素移动到最顶部的位置。
  • 顺时针旋转:将整个网格顺时针旋转 $90$ 度。

给定两个这样的网格,计算将第一个网格变换为第二个网格所需的最少操作次数。

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$ ($1 \le T \le 2 \times 10^5$),表示测试数据的组数。对于每组测试数据:

前三行,第 $i$ 行包含一个字符串 $a_{i,1}a_{i,2}a_{i,3}$ ($1 \le a_{i,j} \le 9$),表示第一个网格的第 $i$ 行。

接下来的三行,第 $i$ 行包含一个字符串 $b_{i,1}b_{i,2}b_{i,3}$ ($1 \le b_{i,j} \le 9$),表示第二个网格的第 $i$ 行。

输出格式

对于每组测试数据,输出一行。如果可以将第一个网格变换为第二个网格,输出一个整数,表示所需的最少操作次数;如果无法做到,则输出 $-1$。

样例

输入格式 1

4
293
678
514
624
579
183
624
579
183
293
678
514
123
456
789
321
456
789
123
456
789
123
456
789

输出格式 1

3
5
-1
0

说明

对于第一个样例测试数据,我们可以先向右平移第三行,然后向下平移第一列,最后顺时针旋转。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.