Score : $500$ points
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:
#
square that is horizontally or vertically adjacent to his current position.a
, ..., or z
.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.
S
, G
, .
, #
, or a lowercase English letter.S
and one square represented as G
.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}$
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.
2 5 S.b.b a.a.G
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:
a
square;11 11 S##...#c... ...#d.#.#.. ..........# .#....#...# #.....bc... #.##......# .......c..# ..#........ a.......... d..#...a... .#........G
14
11 11 .#.#.e#a... .b..##..#.. #....#.#..# .#dd..#..#. ....#...#e. c#.#a....#. .....#..#.e .#....#b.#. .#...#..#.. ......#c#G. #..S...#...
-1