QOJ.ac

QOJ

Limite de temps : 2 s Limite de mémoire : 1024 MB Points totaux : 100

#3128. 华容道谜题

Statistiques

Rush Hour 是一款由 Nob Yoshigahara 在 20 世纪 70 年代发明的益智游戏,目前由 ThinkFun 公司生产。棋盘是一个 $6 \times 6$ 的网格,瓷砖上有凹槽,允许车辆滑动。轿车和卡车的宽度均为一格,但轿车长度为两格,卡车长度为三格。车辆只能在网格上沿直线向前或向后移动。游戏的目标是通过移动其他车辆,将唯一的红色轿车从棋盘的出口完全移出。图 1 展示了一个 Rush Hour 谜题的示例。

图 1:Rush Hour 谜题示例。

我们为谜题中的每辆车分配一个唯一的 $id$,编号从 1 到车辆总数,其中红色轿车的 $id$ 为 1。谜题的棋盘信息由一个名为 $board\ matrix$ 的 $6 \times 6$ 矩阵表示。矩阵中的每个条目是放置在该凹槽上的车辆的 $id$,如果该凹槽上没有车辆,则填入 0。棋盘的出口位于第 3 行的最右侧。图 2 展示了对应于图 1 中谜题的棋盘矩阵。

图 2:对应于图 1 中谜题的棋盘矩阵。

将一个棋子(轿车或卡车)移动一个单位(一个凹槽)称为一步。如果一个谜题可以在不超过 10 步内解决(红色轿车完全移出棋盘出口),则称该谜题为简单的。请编写一个程序来判断一个谜题是否简单。

输入格式

输入包含 6 行,每行表示棋盘矩阵中每一行的内容(6 个整数,以空格分隔)。

输出格式

如果谜题是简单的,输出解决该谜题所需的最少步数,否则输出 -1。

数据范围

  • 每个谜题中最多有 10 辆车。
  • 每个谜题中只有红色轿车可以被移出棋盘。

样例

样例输入 1

2 2 0 0 0 7
3 0 0 5 0 7
3 1 1 5 0 7
3 0 0 5 0 0
4 0 0 0 8 8
4 0 6 6 6 0

样例输出 1

-1

样例输入 2

0 2 0 6 6 0
0 2 0 0 7 0
0 3 1 1 7 0
0 3 4 4 8 0
0 5 5 5 8 0
0 0 0 0 0 0

样例输出 2

6

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.