Portal Maze
The site security team left you a hand-drawn map of the old server bunker. The corridors are a grid of walls and open floor tiles, and scattered along the corridor walls are stenciled two-letter codes marking matched teleport portals. You need the fewest steps from the entrance to the exit.
The map is a grid of R rows and C columns, each cell one of:
#— a wall..— an open floor tile.- a space — empty space outside the corridors (never walkable).
- an uppercase letter
A–Z— part of a stenciled two-letter portal code.
The two letters of a code are always written next to each other, either
horizontally (same row, adjacent columns, read left to right) or
vertically (same column, adjacent rows, read top to bottom). Right next to
every code, on the side that continues past its two letters, there is exactly
one open floor tile . — that tile is the code's portal. Precisely: for a
code occupying cells (r1, c1) and (r2, c2) (with (r2, c2) immediately
after (r1, c1) in reading order), look at the cell immediately before
(r1, c1) and the cell immediately after (r2, c2) (going off the edge of
the grid counts as neither); exactly one of those two cells is ., and
that cell is the code's portal tile. No letter is shared between two
different codes, and no three or more uppercase letters are ever consecutive
in a row or column.
Every code made of two letters appears exactly twice in the grid, marking
a pair of portal tiles that are connected to each other, except AA and
ZZ, which each appear exactly once: AA's portal tile is where you
start, and ZZ's portal tile is where you must reach. AA and ZZ do not
teleport anywhere themselves.
In one step you may either:
- move from your current floor tile to an orthogonally adjacent (up, down, left or right) floor tile, or
- if your current tile is a portal tile that is part of a two-occurrence code, jump directly to its matching portal tile anywhere else in the grid.
Both kinds of step cost 1. Find the minimum number of steps from AA's
portal tile to ZZ's portal tile, or determine it is impossible.
Input
The first line contains two integers R and C (1 ≤ R, C ≤ 500), the
number of rows and columns of the map.
Each of the next R lines describes one row, using the characters above. A
row's line may be shorter than C characters if its remaining characters
would all be spaces — pad it with spaces on the right to length C before
reading it as a grid row. No line is longer than C characters.
It is guaranteed the map is well formed as described: codes are exactly two
letters, read horizontally or vertically, each with exactly one adjacent
portal tile as defined above; AA and ZZ each occur exactly once and every
other code occurs exactly twice.
Output
Print a single integer, the minimum number of steps from AA to ZZ, or
-1 if ZZ cannot be reached.
Notes
In the first example, going around either side of the maze's central block
takes 12 steps, but stepping onto the BC portal partway along cuts it down
to 11: 5 steps to reach the first BC tile, 1 step to teleport through it,
then 5 more steps to ZZ.
The second example has two separate rooms with no shared floor tile and no
portal linking them, so ZZ is unreachable.