š§© DSA Project ā Backtracking
Solve Any Sudoku
Solve Any Sudoku
In Milliseconds
Generate random puzzles or input your own. Watch the backtracking algorithm solve it step-by-step.
Fast
0 msSolve Time
0Steps
0Backtracks
0/81Filled
How It Works
The backtracking algorithm explores possibilities systematically and undoes choices that lead to dead ends.
01
Find Empty Cell
Scan the grid to find the first empty cell (value = 0).
02
Try Values 1ā9
Attempt placing digits 1ā9 and validate against Sudoku constraints.
03
Validate Placement
Check row, column, and 3Ć3 sub-grid for conflicts.
04
Recurse or Backtrack
If valid, recurse. If no digit works, backtrack and try next option.
The Algorithm
backtracking_solver.pseudo
function solveSudoku(board):
for row from 0 to 8:
for col from 0 to 8:
if board[row][col] == 0:
for num from 1 to 9:
if isValid(board, row, col, num):
board[row][col] = num
if solveSudoku(board): return true
board[row][col] = 0 // backtrack
return false
return true // solved!
function isValid(board, row, col, num):
Check row, column, and 3x3 sub-grid
Return false if num already exists
Return true otherwise
Time Complexity
O(9n)
Each empty cell tries up to 9 digits.
Space Complexity
O(n²)
Recursion stack + 9Ć9 board.
Approach
Backtracking
DFS with constraint checking.