AiTechWorlds
AiTechWorlds
Watch N-Queens, Sudoku, Rat in a Maze, and Knightβs Tour solved with backtracking β try, place, and undo animated on a board.
def solve(row, n, cols, d1, d2, board):
if row == n:
return True
for c in range(n):
if c in cols or (row-c) in d1 or (row+c) in d2:
continue
cols.add(c); d1.add(row-c); d2.add(row+c)
if solve(row+1, n, cols, d1, d2, board):
return True
cols.discard(c); d1.discard(row-c); d2.discard(row+c)
return FalseThe N-Queens problem places N queens on an NΓN board so that none attack each other. Backtracking tries each column per row, undoing a placement when it leads to a conflict.
| Time (worst) | O(n!) |
| Space | O(n) |
The Backtracking Visualizer on AiTechWorlds animates how backtracking algorithms explore choices, hit dead ends, and undo decisions to find a valid solution. Backtracking is the engine behind many puzzles and constraint problems, but the recursive try-place-undo cycle is hard to follow in code. This tool animates N-Queens, Sudoku, Rat in a Maze, and the Knight's Tour on a board, showing each attempted placement, each rejection, and each undo as the algorithm searches. You can adjust board size and speed, or step through the recursion manually to see exactly when and why it backtracks. Everything runs in your browser β AiTechWorlds built this for CS students, interview candidates, and educators.
Choose a problem
Pick N-Queens, Sudoku, Rat in a Maze, or Knight's Tour.
Configure the board
Set the board size or starting grid as the problem allows.
Run the solver
Press Solve to animate, or step through each placement and undo.
Watch placements and undos
Valid tries are placed; invalid ones are rejected and removed as it backtracks.
Reach the solution
Follow the search until a complete valid configuration is found.
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 18, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.
Advertisement