Maze Solving with DFS
Grid search with visited cells and path backtracking
Explore one route as deeply as possible
Depth-first search enters an open neighboring cell, marks it visited, and continues from there. Walls, boundaries, and visited cells are rejected. The visited set prevents cycles, while a separate path stack records the cells on the current candidate route.
Dead ends trigger an undo
If a cell has no unvisited open neighbor, remove it from the active path and return to the previous cell. That previous recursive call can then try another direction. Reaching the goal stops the search with the current path intact.
The player uses a fixed 5 by 5 maze. Amber cells form the current DFS path, previously visited cells remain visible, dark cells are walls, and the successful start-to-goal route turns green.
Start at (0, 0) and search for goal (4, 4).
One path, not necessarily the shortest
With a visited set, DFS processes each grid cell at most once, taking O(RC) time and space. It finds a path when one exists, but not necessarily the shortest path. Breadth-first search is the standard unweighted-grid choice when minimum step count matters.
Compare grid backtracking with board constraints in N-Queens and explicit choices in Subsets.