/
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
縦 H マス、横 W マスの 2 次元グリッドで表された街があります。
上から i 行目、左から j 列目のマスの情報が文字 a_{i,j} により与えられます。 a_{i,j} は S , G , . , # , a ~ z のいずれかです。
# は入ることができないマスを、a ~ z はテレポーターのあるマスを表します。
高橋くんははじめ S のマスにおり、 1 秒ごとに以下のいずれかの移動を行います。
- 現在いるマスと上下左右に隣り合う、
#でないマスに移動する。 - 今いるマスと同じ文字が書かれているマスを 1 つ選び、そこにテレポートする。この移動は現在いるマスが
a~zのいずれかであるとき使える。
高橋くんが S のマスから G のマスに移動するのに必要な最短の時間を求めてください。
ただし、どうしても G のマスにたどり着けない場合は、代わりに -1 を出力してください。
制約
- 1 \le H, W \le 2000
- a_{i,j} は
S,G,.,#, 英小文字のいずれか SのマスとGのマスはちょうど 1 つ存在する
入力
入力は以下の形式で標準入力から与えられる。
H W
a_{1,1}\dots a_{1,W}
\vdots
a_{H,1}\dots a_{H,W}
出力
S のマスから G のマスに移動するのに必要な最短時間を出力せよ。
S のマスから G のマスに移動する方法が存在しない場合は、代わりに -1 を出力せよ。
入力例 1
2 5 S.b.b a.a.G
出力例 1
4
上から i 行目、左から j 列目のマスを (i, j) で表すこととします。
はじめ、高橋くんは (1, 1) にいます。
例えば、以下のような手順で 4 秒で (2, 5) に移動することができます。
- (1, 1) から (2, 1) に移動する
- (2, 1) と同じ
aのマスである (2, 3) にテレポートする - (2, 3) から (2, 4) に移動する
- (2, 4) から (2, 5) に移動する
入力例 2
11 11 S##...#c... ...#d.#.#.. ..........# .#....#...# #.....bc... #.##......# .......c..# ..#........ a.......... d..#...a... .#........G
出力例 2
14
入力例 3
11 11 .#.#.e#a... .b..##..#.. #....#.#..# .#dd..#..#. ....#...#e. c#.#a....#. .....#..#.e .#....#b.#. .#...#..#.. ......#c#G. #..S...#...
出力例 3
-1
Score : 500 points
Problem Statement
There is a town represented as a two-dimensional grid with H horizontal rows and W vertical columns.
A character a_{i,j} describes the square at the i-th row from the top and j-th column from the left. Here, a_{i,j} 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
- 1 \le H, W \le 2000
- a_{i,j} is
S,G,.,#, or a lowercase English letter. - There is exactly one square represented as
Sand one square represented asG.
Input
Input is given from Standard Input in the following format:
H W
a_{1,1}\dots a_{1,W}
\vdots
a_{H,1}\dots a_{H,W}
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 (i, j) denote the square at the i-th row from the top and j-th column from the left.
Initially, Takahashi is at (1, 1).
One way to reach (2, 5) in four seconds is:
- go from (1, 1) to (2, 1);
- teleport from (2, 1) to (2, 3), which is also an
asquare; - go from (2, 3) to (2, 4);
- go from (2, 4) to (2, 5).
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