problems / 116

Portal Maze

medium bfs shortest-paths graphs parsing 2000 ms · 256 MB

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.

Example 1

Input

11 11
     A
     A
 ####.####
 #.......#
BC.#####.#
 #.#####.#
 #.#####.BC
 #.......#
 ####.####
     Z
     Z

Output

11

Example 2

Input

4 7
 A   Z
 A   Z
#.#.#.#
#...#.#

Output

-1

Hints · reveal one at a time if you're stuck

Hint 1

Read the map first: each two-letter code sits beside exactly one open tile, which is its portal. Pad short rows with spaces before indexing, then find each code and decide which side holds its floor tile.

Hint 2

Once the portal tiles are paired up, the maze is a grid where every move costs 1, plus a 1-cost teleport between each pair. Think about which search gives shortest distances when every step costs the same.

Hint 3

Run a breadth-first search from the start tile, treating a teleport as an ordinary step to its partner tile, and stop when the goal tile is reached. If the queue empties first, print -1.

report an issue Prefer your own editor? sooks prep 116 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.