An interactive visualization of BFS wavefront expansion on an N×N binary grid. Watch the algorithm explore all 8 directions level by level and find the shortest clear path from top-left to bottom-right.
BFS LeetCode 1091 ↗ Mediumn x n binary matrix grid, return the length of the shortest
clear path in the matrix. If there is no clear path, return -1.(0, 0) to the
bottom-right cell (n-1, n-1) such that:0 (open).fun shortestPathBinaryMatrix(grid: Array<IntArray>): Int { val n = grid.size if (grid[0][0] != 0 || grid[n-1][n-1] != 0) return -1 val queue: ArrayDeque<Triple<Int,Int,Int>> = ArrayDeque() queue.add(Triple(0, 0, 1)) grid[0][0] = 1 // mark visited val dirs = arrayOf(intArrayOf(-1,-1),intArrayOf(-1,0),intArrayOf(-1,1), intArrayOf(0,-1), intArrayOf(0,1), intArrayOf(1,-1), intArrayOf(1,0), intArrayOf(1,1)) while (queue.isNotEmpty()) { val (r, c, dist) = queue.removeFirst() if (r == n-1 && c == n-1) return dist for (d in dirs) { val nr = r + d[0]; val nc = c + d[1] if (nr in 0 until n && nc in 0 until n && grid[nr][nc] == 0) { grid[nr][nc] = 1 queue.add(Triple(nr, nc, dist+1)) } } } return -1}
BFS explores cells level by level (wavefront expansion). Since every move costs 1 step,
the first time BFS reaches the destination (n-1, n-1), the distance is guaranteed
to be the shortest path. This is the fundamental property of BFS on unweighted graphs.
grid[0][0] or grid[n-1][n-1] is blocked (1), return -1 immediately.(0, 0) with distance 1, and mark it visited.(r, c, dist).(n-1, n-1): return dist.dist + 1.
Unlike typical grid problems that use 4-directional movement (up/down/left/right), this problem
allows diagonal movement. This means from any cell, we can move to any of its
8 neighbors — including diagonals. This is why the directions array includes
(-1,-1), (-1,1), (1,-1), (1,1) in addition to the 4 cardinal directions.
Each cell is visited at most once, and for each cell we check 8 neighbors — so the total work is
O(n²). The queue and visited markers both use O(n²) space.