Maze Search with Backtracking
Trace the current path and identify the branch to undo when it reaches a dead end.
Core Intuition & How It Works
Backtracking maze search explores corridors recursively, marking visited cells to avoid loops and unmarking them when backtracking if all paths must be enumerated. For shortest exit distance, BFS is preferred over DFS backtracking.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(4^(R×C)) all paths, O(R×C) single path |
| Space Complexity | O(R × C) |
| Topic Chapter | Chapter 14: Backtracking & Computational Complexity |
| Difficulty Level | Advanced |
Key Pattern to Recognize
Prune out-of-bounds, wall, and visited cells at entry, and restore cell state on exit when searching for multiple paths.
Common Pitfalls & Edge Cases
Not restoring visited state when searching for all valid paths, or restoring it unnecessarily when only reachability is needed.
Real-World & Technical Interview Applications
Word search on grids, robotics path exploration, puzzle solvers (Sudoku), and game AI.