← Visualizers
Graphs · Grid BFS, unweighted shortest path LeetCode 1091

Shortest Path in Binary Matrix / visualized

A BFS level is a distance. Eight directions, a size snapshot taken before each level, and the one line the whole pattern rests on: mark on push, in the same statement that enqueues. Flip the toggle to run the mark-on-pop version and watch the queue.

Execution

idle
Press Run to begin.
0 / 0
Speed

Java · running line

popped now in the queue settled target blocked
O(n²) time, O(n²) space  ·  marking on pop is not merely slower. The same cell is enqueued once per neighbour that reaches it, each push overwrites its recorded distance with a longer one, and the answer comes back too large — failure mode 1.1, a distance greater than the true minimum.