QOJ.ac

QOJ

시간 제한: 1 s 메모리 제한: 256 MB 총점: 100

#17440. 迷宫

통계

考虑以下由等边三角形组成的迷宫:

每个顶点由两个坐标 $x$ 和 $y$ 描述,如上图所示。某些边上有一个白色或黑色的圆圈。控制在迷宫中移动有两个主要规则:

  • 只允许通过上面有圆圈的边。
  • 在迷宫中行走时,必须交替经过白色和黑色的圆圈;也就是说,只有当上一个经过的圆圈是黑色时,才允许通过带有白色圆圈的边,反之亦然。在第一步移动时,允许通过带有黑色或白色圆圈的边。

任务

编写一个程序,寻找从迷宫入口点到出口点的最短路径长度。路径长度定义为通过的边(或圆圈)的数量。你可以假设这样的路径总是存在。

输入格式

第一行包含两个整数 $W$ 和 $H$,分别表示迷宫的宽度和高度($1 \le W, H \le 500$)。

第二行包含四个整数值:$X_1, Y_1, X_2, Y_2$($0 \le X_1, X_2 \le W$;$0 \le Y_1, Y_2 \le H$)。其中 $(X_1, Y_1)$ 是迷宫入口点的坐标,$(X_2, Y_2)$ 是出口点的坐标。

接下来的 $2H+1$ 行提供了边的描述:奇数行(第 3 行、第 5 行等)描述水平边,偶数行(第 4 行、第 6 行等)描述非水平边。每行由一个由字符 nwb 组成的字符串构成,其中 n 表示该边上没有圆圈,wb 分别表示该边上有白色或黑色的圆圈。这些字符之间没有空格。显然,奇数行恰好包含 $W$ 个字符,偶数行恰好包含 $2W+1$ 个字符。

输出格式

输出仅一行,包含一个整数,表示从迷宫入口点到出口点的最短路径长度。

样例

输入 1

2 1
0 0 2 1
bb
nwwnw
bn

输出 1

6

说明 1

一个简单的迷宫。一条可能的最短路径如下:

$(0, 0) \to (1, 0) \to (0, 1) \to (1, 1) \to (1, 0) \to (2, 0) \to (2, 1)$

以下是该迷宫和最短路径的示意图:

输入 2

5 4
0 2 5 2
nnbnn
nnnwwbwnnnn
nbbbn
nnwbwwbwwnn
bwwww
nnbwbbwwbnn
nwwwn
nnnnbwbbnnn
nnwnn

输出 2

22

说明 2

这是第一页图片中给出的迷宫的描述。最短路径如下:

$(0, 2) \to (1, 2) \to (1, 1) \to (2, 1) \to (2, 0) \to (3, 0) \to (3, 1) \to (3, 2) \to (4, 1) \to (3, 1) \to (3, 0) \to (2, 0) \to (2, 1) \to (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 4) \to (3, 4) \to (3, 3) \to (4, 3) \to (4, 2) \to (5, 2)$

(长度:22)

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.