AiTechWorlds
AiTechWorlds
Visualize backtracking find a path through a maze, exploring directions and undoing dead ends, with synced code.
def solve(r, c, m, sol):
n = len(m)
if r==n-1 and c==n-1 and m[r][c]:
sol[r][c] = 1; return True
if r<0 or c<0 or r>=n or c>=n or not m[r][c] or sol[r][c]:
return False
sol[r][c] = 1
if solve(r+1,c,m,sol) or solve(r,c+1,m,sol):
return True
sol[r][c] = 0 # backtrack
return FalseRat in a Maze finds a path from the top-left to the bottom-right of a grid with walls, exploring directions recursively and backtracking from dead ends.
| Time (worst) | O(4^(n²)) |
| Space | O(n²) |
The Rat in a Maze Visualizer animates how backtracking finds a path from the top-left to the bottom-right of a grid with walls. It marks a cell as part of the path, recurses into open neighbors, and backtracks from dead ends by unmarking the cell. Watch the path grow and shrink as the solver explores.
Press Play
Watch the rat explore open cells and backtrack from dead ends.
Step through
Use Step Forward/Back or the timeline to follow each move.
Read the code
View the Rat in a Maze backtracking code in C++, Java, Python, or JavaScript and copy it.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.