DSAverse
Backtracking
Loading Backtracking...
N-Queens Board
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Backtracking
Loading Backtracking...
N-Queens Board
Navigate from the top-left to the bottom-right of a binary maze. Backtracking explores all paths, marking visited cells and retreating from dead ends.
1 / 0
Click Play to begin.
In what order does the standard Rat in a Maze algorithm try moves?
def solve_maze(maze):
n = len(maze)
path = []
def dfs(r, c):
# Out of bounds, wall, or already visited
if (r < 0 or r >= n or c < 0 or c >= n
or maze[r][c] == 0
or (r, c) in set(path)):
return False
path.append((r, c))
if r == n - 1 and c == n - 1:
return True # reached exit
# Try Down, Right, Up, Left
for dr, dc in [(1,0),(0,1),(-1,0),(0,-1)]:
if dfs(r + dr, c + dc):
return True
path.pop() # backtrack
return False
if dfs(0, 0):
return path # solution path
return None # no solution