AAlgoLab
ALG-M017MediumBreadth-First Search

Shortest Warehouse Walk

In a rectangular grid, 0 means walkable and 1 blocked. Find the minimum number of four-direction moves from top-left to bottom-right. Return -1 if impossible.

Contract

input grid: integer[][]
return integer

Constraints

  • • Grid entries are 0 or 1
  • • All rows equal length

Examples

Example 1
grid = [[0, 0, 1], [1, 0, 0], [1, 1, 0]]
→ 4
Example 2
grid = [[0, 1], [1, 0]]
→ -1

5 judge tests (2 shown above). Target complexity: O(rows*cols).