Watch Breadth-First Search (BFS) and Depth-First Search (DFS) find paths through complex grids. Interactive matrix explorations.
About this algorithm
This page finds the shortest way out of a maze by flooding it. Water poured in at the start spreads one cell per beat into every open neighbour, so all cells at distance d fill together, before any cell at distance d + 1. The first drop to touch the exit has come the shortest way, and following each cell back to the cell that filled it recovers that route.
Use a BFS wavefront when every move costs the same and you want the fewest moves from one cell to another, plus the distance to every cell along the way: escaping a maze, the nearest exit in a building plan, or the number of moves a game piece needs. The matrix BFS page runs the same search on a random board you can redraw by clicking; this page keeps one fixed maze so the distance layers and the traced route are the same on every run. The matrix DFS page also reaches the exit, but the route it returns can be much longer than needed, so it answers "is there a way out" rather than "what is the shortest one". If moves cost different amounts, such as mud that takes three turns to cross, layers no longer match cost; use Dijkstra from the graph pages, which always expands the cheapest cell next.
The start cell gets distance 0 and goes in the queue, and it is marked seen so nothing can fill it twice.
For every cell in the current layer, look right, down, left and up. A neighbour that is open and not yet seen gets the next distance, remembers which cell filled it, and joins the new layer. Walls and the board edge stop the water. The new layer then replaces the old one as the wavefront.
When the exit joins a layer, its distance is the length of the shortest route, because every shorter distance has already been filled without reaching it. If a layer comes up empty first, the exit is sealed off and there is no way out.
Start at the exit and keep stepping to the cell that filled it. Each step lands one layer closer to the start, so after exactly that many steps you are at the start. Reverse the list for the route from start to exit.
The maze has 15 rows and 24 columns, 109 wall cells and 251 open ones. The start is at row 9, column 1 and the exit at row 4, column 22 (counting from 0). Ignoring walls the exit is 26 moves away, but a wall runs down column 10 from row 8 to row 12 and a bar fills row 9 from column 6 to 12, so the water has to go round. Layers 1 to 33 hold between 4 and 11 cells each. Water reaches the exit on layer 34, arriving round the bottom of the maze and over the top on the same layer, with 249 cells wet and only 2 dry, the two cells to the right of the exit. The traced route runs right along row 9, drops down to row 13 to pass under the column 10 wall, climbs column 13 back to row 10, then works east and north to the exit: 34 moves. The maze has 61,675 different routes of that length; parent links pick one. The run is 45 frames: the dry maze, the pour, 33 layers, the exit, 8 frames of tracing and the final route. The board is fixed and clicking does not change it.
Each wet cell keeps a colour for its distance from the start: deep blue near the start, light cyan far from it. The newest layer is drawn brightest, so you can see the front bending round walls. Two cells with the same colour are the same number of moves from the start. Once the exit is found the water dims and a green line runs from the exit back to the start, then flashes. The step log counts each layer, "12 steps out", with how many cells it added.
Time: every open cell fills at most once and checks four neighbours, and the trace back is at most one step per cell, so the work grows with R × C. Space: the seen marks and parent links take one entry per cell, and a single layer of the queue can hold a long line of cells.