AiTechWorlds
AiTechWorlds
Animate the knight's tour backtracking solver visiting every square once, with synced multi-language code.
MOVES = [(2,1),(1,2),(-1,2),(-2,1),(-2,-1),(-1,-2),(1,-2),(2,-1)]
def tour(r, c, step, n, b):
b[r][c] = step
if step == n*n: return True
for dr, dc in MOVES:
nr, nc = r+dr, c+dc
if 0<=nr<n and 0<=nc<n and b[nr][nc]==0:
if tour(nr, nc, step+1, n, b): return True
b[r][c] = 0 # backtrack
return FalseThe Knight's Tour finds a sequence of knight moves that visits every square of a board exactly once, using backtracking to undo moves that lead to a dead end.
| Time (worst) | O(8^(n²)) |
| Space | O(n²) |
The Knight's Tour Visualizer animates how backtracking finds a sequence of knight moves that visits every square of a board exactly once. The knight tries each of its up to eight moves, numbers the square, and backtracks when no unvisited move remains. Watch the tour build and undo moves at dead ends.
Press Play
Watch the knight move and number each square it visits.
Step through
Use Step Forward/Back or the timeline to study each move and backtrack.
Read the code
View the Knight's Tour 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.