Fewest Moves Through a Maze

MediumGraphs≈ 35 min

A maze is given as a list of equal-length strings: S is the start, E the exit, # a wall and . open floor. Each move goes one cell up, down, left or right, never through a wall or off the grid.

Return the fewest moves needed to walk from S to E, or -1 if the exit cannot be reached.

Examples

text
["S.#",
 "..#",
 "#.E"]          →  4

["S#E"]          →  -1

["S...",
 "###.",
 "E..."]         →  8

In the first example the walk goes down, right, down, right.

Constraints

  • Up to 100 rows and 100 columns, with exactly one S and one E.
  • Aim for O(n) in the number of cells: each cell should be reached once.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 3 visible cases here, in your browser. Submit runs all 7 on the judge, 4 of them hidden, and measures how your solution scales.