← Back to challenges

The Shortest Path

JavaScriptHardarrayslogicloops

Instructions

Given a rectangular grid of m by n spaces, signaled by 0s, and a number of points, signaled by 1, 2, 3..., return the number of moves for the shortest path that starts at 1 and goes over all the other points in ascending order.

Examples

shortestPath([
  "001",
  "002",
  "003"
]) ➞ 2

shortestPath([
  "00000",
  "01006",
  "02000",
  "30050",
  "00004"
]) ➞ 13

shortestPath([
  "00020000",
  "01000000"
]) ➞ 3

Notes

  • Only horizontal and vertical movements are allowed.
  • All movements from one place to an adjacent one count as 1 regardless of direction.
  • The points range from 1 to at most 9 with no repeating or missing digits.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.