Shortest Path in a Binary Matrix
Problem statement
Given an n x n binary matrix, return the number of cells in the shortest clear path from the top-left cell to the bottom-right cell.
A clear path visits only cells containing 0 and may move horizontally, vertically, or diagonally to any of the eight neighboring cells. Return -1 when no clear path exists.
Function
shortestPathBinaryMatrix(grid: int[][]) → intExamples
Example 1
grid = [[0,1],[1,0]]return = 2The two open corner cells are diagonal neighbors.
Example 2
grid = [[1,0],[0,0]]return = -1The starting cell is blocked.
Constraints
1 <= n <= 100grid.length == grid[i].length == n- Every cell is
0or1.