medium

Shortest Path in Binary Matrix

In an n × n grid of 0s (open) and 1s (blocked), find the length of the shortest path from the top-left to the bottom-right cell moving in any of the 8 directions through open cells only. Length counts cells visited; return -1 if there is no path.

Constraints
  • 1 ≤ n ≤ 100
  • grid[i][j] ∈ {0, 1}
Examples
in: grid = [[0,0,0],[1,1,0],[1,1,0]]
out: 4
in: grid = [[0,1],[1,0]]
out: 2

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.