🧩 DSA Project — Backtracking

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
Ready — Generate or enter a puzzle

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.