Shortest Path in Binary Matrix
Given an n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1. A clear path is a path from the top-left cell (0, 0) to the bottom-right cell (n-1, n-1) such that all the visited cells are 0. You may move 8-directionally (horizontal, vertical, and diagonal).

30:00

Shortest Path in Binary Matrix
medium
Topics
Companies

Given an n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1. A clear path is a path from the top-left cell (0, 0) to the bottom-right cell (n-1, n-1) such that all the visited cells are 0. You may move 8-directionally (horizontal, vertical, and diagonal).

Example 1:
Input: [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
Constraints:
  • n==grid.length==grid[i].lengthn == \text{grid.length} == \text{grid}[i].\text{length}

  • 1n1001 \leq n \leq 100

  • grid[i][j]{0,1}\text{grid}[i][j] \in \{0, 1\}

Input
arr =[[0,0,0],[1,1,0],[1,1,0]]

Start BFS from (0,0)

Grid (0=open, 1=blocked)

1

BFS Queue

(0,0)

Shortest Path: No Path

Start/End

Current

In Queue

Path

Variables
No variables to display
DepthFunction Call
Stack empty
0/6