Shortest Path in Binary Matrix
In an N by N square grid, each cell is either empty (0) or blocked (1).
A clear path from top-left to bottom-right has lengthkif and only if it is composed of cellsC_1, C_2, ..., C_k such that:
Adjacent cells
C_iand
C_{i+1}are connected 8-directionally (ie., they are different and share an edge or corner)
C_1is at location
(0, 0)(ie. has value
grid[0][0])
C_kis at location
(N-1, N-1)(ie. has value
grid[N-1][N-1])
If
C_iis located at
(r, c), then
grid[r][c]is empty (ie.
grid[r][c] == 0).
Return the length of the shortest such clear path from top-left to bottom-right. If such a path does not exist, return -1.
Last updated
Was this helpful?