Dodomeki
Sign in

80. Shortest Path in a Grid

Medium

In a binary matrix, 0 is an open cell and 1 is blocked. Starting from the top-left corner, return the minimum number of cells visited to reach the bottom-right corner, moving only up, down, left, or right. Return -1 if no path exists.

Example 1
Input: grade = [[0,0,0],[1,1,0],[1,1,0]]
Output: 5
Example 2
Input: grade = [[0,1],[0,0]]
Output: 3
Constraints
  • 1 <= linhas, colunas <= 100
  • A grade contém apenas 0 e 1.
GraphsBFSMatrix
Caso 1
grade =
[[0,0,0],[1,1,0],[1,1,0]]
Expected =
5
Caso 2
grade =
[[0,1],[0,0]]
Expected =
3

Run checks these 2 cases. Submit runs all 6, including hidden ones.