#abc184e. E - Third Avenue
E - Third Avenue
Score : points
问题陈述
有一个城镇,表示为一个二维网格,有 行水平行和 列垂直列。
一个字符 描述了从顶部数第 行和从左边数第 列的正方形。在这里, 是以下之一:S
,G
,.
,#
,a
,...,z
。
#
表示一个不能进入的正方形,而 a
,...,z
表示有传送门的正方形。
高桥最初在表示为 S
的正方形。在每一秒钟,他将进行以下移动之一:
- 移动到当前位置水平或垂直相邻的非
#
方块。 - 选择一个与当前位置相同字符的正方形,并传送到那个正方形。只有当他在一个表示为
a
,...,或z
的正方形时,他才能使用这个移动。
找出高桥从表示为 S
的正方形到达表示为 G
的正方形所需的最短时间。
如果目的地无法到达,请报告 -1
。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
There is a town represented as a two-dimensional grid with horizontal rows and vertical columns.
A character describes the square at the -th row from the top and -th column from the left. Here, is one of the following: S
, G
, .
, #
, a
, ..., and z
.
#
represents a square that cannot be entered, and a
, ..., z
represent squares with teleporters.
Takahashi is initially at the square represented as S
. In each second, he will make one of the following moves:
- Go to a non-
#
square that is horizontally or vertically adjacent to his current position. - Choose a square with the same character as that of his current position, and teleport to that square. He can only use this move when he is at a square represented as
a
, ..., orz
.
Find the shortest time Takahashi needs to reach the square represented as G
from the one represented as S
.
If the destination is unreachable, report -1
instead.
Constraints
- is
S
,G
,.
,#
, or a lowercase English letter. - There is exactly one square represented as
S
and one square represented asG
.
Input
Input is given from Standard Input in the following format:
Output
Print the shortest time Takahashi needs to reach the square represented as G
from the one represented as S
.
If the destination is unreachable from the initial position, print -1
instead.
Sample Input 1
2 5
S.b.b
a.a.G
Sample Output 1
4
Let denote the square at the -th row from the top and -th column from the left.
Initially, Takahashi is at . One way to reach in four seconds is:
- go from to ;
- teleport from to , which is also an
a
square; - go from to ;
- go from to .
Sample Input 2
11 11
S##...#c...
...#d.#.#..
..........#
.#....#...#
#.....bc...
#.##......#
.......c..#
..#........
a..........
d..#...a...
.#........G
Sample Output 2
14
Sample Input 3
11 11
.#.#.e#a...
.b..##..#..
#....#.#..#
.#dd..#..#.
....#...#e.
c#.#a....#.
.....#..#.e
.#....#b.#.
.#...#..#..
......#c#G.
#..S...#...
Sample Output 3
-1