DSAverse
Recursion
Loading Recursion Visualizer...
Recursion Tree
depth: 1f(n) = f(n-1) + f(n-2)
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Recursion
Loading Recursion Visualizer...
Recursion Tree
depth: 1f(n) = f(n-1) + f(n-2)
Watch recursive backtracking explore every path, intelligently retreat from dead ends, and find the solution.
Click Play to begin solving the maze.
What does the maze solver do when it hits a dead end?
def solve_maze(maze, row, col, end_row, end_col, visited, path):
visited[row][col] = True
path.append((row, col))
if row == end_row and col == end_col:
return True # Solution found
# Try all 4 directions: right, down, left, up
for dr, dc in [(0,1), (1,0), (0,-1), (-1,0)]:
new_row, new_col = row + dr, col + dc
if is_valid(new_row, new_col, maze, visited):
if solve_maze(maze, new_row, new_col,
end_row, end_col, visited, path):
return True
# Backtrack: undo this cell
path.pop()
visited[row][col] = False
return False
def is_valid(row, col, maze, visited):
return (0 <= row < len(maze) and
0 <= col < len(maze[0]) and
maze[row][col] == 0 and
not visited[row][col])