Shortest Path in a Grid

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Write shortestPath(grid): grid is an array of strings of equal length, "." open and "#" wall. Return the fewest steps from the top-left square to the bottom-right one, moving up, down, left or right through open squares, or -1 if there's no way through (or either corner is a wall). Explore outwards one step at a time (breadth-first search): trying every route is far too slow for the 200 by 200 grid in the checks.

Your code must pass

  • Finds the fewest steps across an open grid
  • Goes round walls
  • Returns -1 when there's no way through
  • Is fast enough for a 200 by 200 grid