AiTechWorlds
AiTechWorlds
Animate the N-Queens backtracking solver placing queens row by row and backtracking on conflicts, with synced code.
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 N-Queens Visualizer animates how backtracking solves the N-Queens problem: it places one queen per row, trying each column, and backtracks whenever a queen would be attacked along a column or diagonal. Solving N-Queens is O(n!) in the worst case. Watch each try, placement, and backtrack with a live decision counter.
Press Play
Watch queens placed row by row; conflicts trigger a backtrack.
Step through
Use Step Forward/Back or the timeline to study each decision.
Read the code
View the N-Queens 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.