0
0
DSA Cprogramming~10 mins

Sudoku Solver Using Backtracking in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Sudoku Solver Using Backtracking
Start with empty cell
Try number 1 to 9
Is number valid?
NoTry next number
Yes
Place number
Move to next empty cell
Repeat until board full
If stuck, backtrack
Remove last number
Back to Try number 1 to 9
The solver tries numbers 1-9 in empty cells, places valid ones, moves forward, and backtracks if stuck.
Execution Sample
DSA C
for (int num = 1; num <= 9; num++) {
  if (valid(board, row, col, num)) {
    board[row][col] = num;
    if (solve(board)) {
      return true;
    }
    board[row][col] = 0;
  }
}
return false;
This code tries numbers 1-9 in a cell, places a valid number, recurses, and backtracks if needed.
Execution Table
StepOperationCell (row,col)Number TriedValid?ActionBoard State Snapshot
1Find empty cell(0,0)--Empty cell found[0,0,0,...] (rest of row 0)
2Try number(0,0)1YesPlace 1[1,0,0,...]
3Find empty cell(0,1)--Empty cell found[1,0,0,...]
4Try number(0,1)1NoTry next number[1,0,0,...]
5Try number(0,1)2YesPlace 2[1,2,0,...]
6Find empty cell(0,2)--Empty cell found[1,2,0,...]
7Try number(0,2)1NoTry next number[1,2,0,...]
8Try number(0,2)3YesPlace 3[1,2,3,...]
9Find empty cell(0,3)--Empty cell found[1,2,3,0,...]
10Try number(0,3)1NoTry next number[1,2,3,0,...]
11Try number(0,3)4YesPlace 4[1,2,3,4,...]
12Find empty cell(0,4)--Empty cell found[1,2,3,4,0,...]
13Try number(0,4)1NoTry next number[1,2,3,4,0,...]
14Try number(0,4)5YesPlace 5[1,2,3,4,5,...]
15Find empty cell(0,5)--Empty cell found[1,2,3,4,5,0,...]
16Try number(0,5)1NoTry next number[1,2,3,4,5,0,...]
17Try number(0,5)6YesPlace 6[1,2,3,4,5,6,...]
18Find empty cell(0,6)--Empty cell found[1,2,3,4,5,6,0,...]
19Try number(0,6)1NoTry next number[1,2,3,4,5,6,0,...]
20Try number(0,6)7YesPlace 7[1,2,3,4,5,6,7,...]
21Find empty cell(0,7)--Empty cell found[1,2,3,4,5,6,7,0,...]
22Try number(0,7)1NoTry next number[1,2,3,4,5,6,7,0,...]
23Try number(0,7)8YesPlace 8[1,2,3,4,5,6,7,8,...]
24Find empty cell(0,8)--Empty cell found[1,2,3,4,5,6,7,8,0]
25Try number(0,8)1NoTry next number[1,2,3,4,5,6,7,8,0]
26Try number(0,8)9YesPlace 9[1,2,3,4,5,6,7,8,9]
27Find empty cellNone--No empty cells, puzzle solved[1,2,3,4,5,6,7,8,9,...]
💡 No empty cells left, puzzle solved successfully
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8After Step 11After Step 14After Step 17After Step 20After Step 23After Step 26Final
board[0][0]01111111111
board[0][1]00222222222
board[0][2]00033333333
board[0][3]00004444444
board[0][4]00000555555
board[0][5]00000066666
board[0][6]00000007777
board[0][7]00000000888
board[0][8]00000000099
Key Moments - 3 Insights
Why do we try numbers 1 to 9 in each empty cell?
Because Sudoku rules allow numbers 1-9 only, and we must find a valid number that fits without conflicts, as shown in steps 2, 4, 5, etc.
What happens when no number is valid for a cell?
We backtrack by removing the last placed number and trying the next number, as implied in the concept flow and seen when 'Try number' results in 'No' and we try next numbers.
How do we know when the puzzle is solved?
When there are no empty cells left to fill, as in step 27 where 'Find empty cell' returns None and the puzzle is complete.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what number is placed in cell (0,5) at step 17?
A6
B5
C1
D0
💡 Hint
Check the 'Number Tried' and 'Action' columns at step 17 in the execution table.
At which step does the solver find no empty cells and conclude the puzzle is solved?
AStep 26
BStep 27
CStep 25
DStep 24
💡 Hint
Look for the row where 'Find empty cell' operation returns 'None' in the execution table.
If the number 1 was invalid in cell (0,1), what does the solver do next according to the execution table?
APlaces number 1 anyway
BTries the next number
CBacktracks immediately
DStops solving
💡 Hint
See steps 4 and 5 where number 1 is invalid and the solver tries number 2 next.
Concept Snapshot
Sudoku Solver Using Backtracking:
- Find an empty cell
- Try numbers 1 to 9
- Check if number is valid (no conflicts in row, column, box)
- Place number if valid and recurse
- If stuck, backtrack by removing number
- Repeat until board is full
Full Transcript
This visualization shows how a Sudoku solver uses backtracking to fill empty cells. It tries numbers 1 to 9 in each empty cell, checks if the number fits Sudoku rules, places it if valid, and moves to the next empty cell. If no number fits, it backtracks by removing the last placed number and tries the next possible number. The process repeats until the board is completely filled with valid numbers. The execution table tracks each step, the cell being filled, the number tried, whether it was valid, the action taken, and the board state snapshot. Variable tracking shows how the board cells change over time. Key moments clarify why numbers 1-9 are tried, what backtracking means, and how the solver knows when the puzzle is solved. The quiz tests understanding of specific steps and solver behavior.