QOJ.ac

QOJ

时间限制: 1 s 内存限制: 1024 MB 总分: 100

#5609. 希尔伯特的树篱迷宫

统计

David 在他的花园里有精心设计的树篱迷宫。对于他每一个不同大小的迷宫,他都想知道在迷宫内及周围任意两个位置之间行走需要多长时间。阶数为 $n \ge 1$ 的树篱迷宫是根据非常具体的指令构建的:

  1. 从 $S = A$ 开始。
  2. 重复 $n$ 次: (a) 创建一个新字符串 $T$,将 $S$ 中的每个 $A$ 替换为 $LBFRAFARFBL$,将每个 $B$ 替换为 $RAFLBFBLFAR$。 (b) 令 $S = T$。
  3. 从 $S$ 中删除所有的 $A$ 和 $B$。

生成的字符串给出了构建树篱迷宫的指令。在无界平面上,从坐标 $(0, 0)$ 出发,面向 $(1, 0)$ 方向。每遇到一个 $F$ 则向前移动一个单位,每遇到一个 $R$ 则向右转 90 度,每遇到一个 $L$ 则向左转 90 度。

构建迷宫后,将位置 $(x, y)$ 识别为由坐标 $(x, y), (x + 1, y), (x, y + 1), (x + 1, y + 1)$ 围成的单位正方形单元格。当且仅当 $|x_1 - x_2| = 1$ 或 $|y_1 - y_2| = 1$(但不能同时满足)且这两个单元格之间没有树篱时,我们可以在两个位置 $(x_1, y_1)$ 和 $(x_2, y_2)$ 之间直接移动。这两个位置之间的距离为 1。

给定数字 $n$ 和一对位置,求这两个位置之间的最短距离。

样例输入中第一个案例的最短路径。

输入格式

输入以一个正整数 $k$ ($1 \le k \le 100$) 开头,表示测试用例的数量。接下来的 $k$ 行,每行包含 5 个整数 $n, x_1, y_1, x_2, y_2$,满足 $1 \le n \le 50$ 且 $-2^{52} \le x_1, y_1, x_2, y_2 \le 2^{52}$。各测试用例之间互不影响。每个用例指定了一个位于无界平面上的独立树篱迷宫。

输出格式

输出 $k$ 行,每行一个整数,对应每个测试用例中两个位置之间的最短距离。

样例

样例输入 1

2
3 5 3 4 0
2 4 3 0 2

样例输出 1

16
11

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.