DSAVisualiseData Structures & Algorithms Studio
Paths & Grids Hard

Shortest Maze Path

Breadth-first search explores a grid in layers and records a predecessor for each discovered cell, allowing it to reconstruct a shortest route.

Trace input

Bounded JSON

Edit the lesson input, then build a deterministic source-linked trace.

This lesson validates its own input and operation limits.

Source scenarios

Complexity

BestO(V + E)
AverageO(V + E)
WorstO(V + E)
SpaceO(V)

Array state

Follow the active comparisons and writes.

Live snapshot

Current operation

Trace state
Ready to explore
Source behavior & common mistakes

Find a shortest four-direction path through a rectangular 0/1 grid and return its row-major cell indexes, following the pinned Java neighbor order.

  • The executable neighbor order is up, left, down, right, even though the source comment lists up, right, down, left.
  • A cell is marked before it is enqueued, so it cannot enter the queue twice.
  • The current neighbor loop finishes after found becomes true; the outer BFS loop then stops.
  • The source does not special-case start equal to end, so it explores the reachable component before returning the one-cell path.

Algorithm code

Java
Maze.javaStep linked
The highlighted line follows the current step.

Ready to begin

Press Play to see what happens.

Detailed execution
Snapshot 1 / 1