1091. Shortest Path in Binary Matrix

Practice Shortest Path in Binary Matrix for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

grid is an n x n matrix of 0 (open) and 1 (blocked). Find the shortest clear path from the top-left cell to the bottom-right cell: every cell on it is 0, and consecutive cells touch by an edge or a corner (8 directions). The length of a path is the number of cells it visits.

Return the length of the shortest clear path, or -1 if there is none.

Example

grid = [[0,0,0],
        [1,1,0],
        [1,1,0]]  ->  4
grid = [[1,0,0],
        [1,1,0],
        [1,1,0]]  ->  -1

Constraints