QOJ.ac

QOJ

حد الوقت: 2 s حد الذاكرة: 2048 MB مجموع النقاط: 100

#2901. 跳房子 500

الإحصائيات

还记得 NAC 2020 的那个新艺术装置吗?那位艺术家又回来了,这次规模更大,而这件新艺术品依然激发了你的灵感——去玩一个幼稚的游戏。该艺术装置由一个铺满方格瓷砖的地面组成。每块瓷砖上都写有一个从 $1$ 到 $k$ 的数字。

你想在上面玩跳房子游戏!你希望从某块标号为 $1$ 的瓷砖开始,然后跳到一块标号为 $2$ 的瓷砖,接着是 $3$,以此类推,直到你到达一块标号为 $k$ 的瓷砖。

不同于通常的欧几里得距离,我们将位于 $(x_1, y_1)$ 的瓷砖与位于 $(x_2, y_2)$ 的瓷砖之间的距离定义为: $$\min [(x_1 - x_2)^2, (y_1 - y_2)^2]$$

你希望使用这种新的距离度量,跳出总距离最短的路径。注意,没有跳跃的路径也是一条路径,其长度为 $0$。请问最短路径的长度是多少?

输入格式

第一行包含两个空格分隔的整数 $n$ ($1 \le n \le 500$) 和 $k$ ($1 \le k \le n^2$),其中艺术装置由一个 $n \times n$ 的矩阵组成,瓷砖上的数字从 $1$ 到 $k$。

接下来的 $n$ 行,每行包含 $n$ 个空格分隔的整数 $x$ ($1 \le x \le k$)。这些是艺术装置上的数字。

输出格式

输出一个整数,表示使用该距离度量从任意一块标号为 $1$ 的瓷砖到任意一块标号为 $k$ 的瓷砖的最短路径总长度,如果不存在这样的路径,则输出 $-1$。

样例

样例输入 1

10 5
5 1 3 4 2 4 2 1 2 1
4 5 3 4 1 5 3 1 1 4
4 2 4 1 5 4 5 2 4 1
5 2 1 5 5 3 5 2 3 2
5 5 2 3 2 3 1 5 5 5
3 4 2 4 2 2 4 4 2 3
1 5 1 1 2 5 4 1 5 3
2 2 4 1 2 5 1 4 3 5
5 3 2 1 4 3 5 2 3 1
3 4 2 5 2 5 3 4 4 2

样例输出 1

0

样例输入 2

10 30
18 13 30 15 18 16 14 1 5 5
17 18 7 30 14 30 13 14 1 28
28 24 7 23 9 10 5 12 21 6
11 16 6 2 27 14 1 26 7 21
16 2 9 26 6 24 22 12 8 16
17 28 29 19 4 6 21 19 6 22
11 27 11 26 13 23 10 3 18 6
14 19 9 8 17 6 16 22 24 1
12 19 10 21 1 8 20 24 29 21
21 29 1 23 23 24 6 20 25 17

样例输出 2

19

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.