Write a program to solve a Sudoku puzzle by filling the empty cells ('.'). Each digit 1-9 must occur exactly once in each row, each column, and each of the nine 3×3 sub-boxes.
Standard 9×9 board with 51 empty cellsFilled 9×9 board (unique solution)'.' | Guaranteed unique solution.m = number of empty cells
Classic backtracking on a fixed 9×9 grid. For each empty cell, try valid digits 1-9. If recursion succeeds, propagate true up the call stack. If no digit works, undo (set cell back to '.') and return false to trigger backtracking in the caller. The isValid check covers row, column, and 3×3 box simultaneously in O(9) time.
Trying all digits in all empty cells means 9 to the power of about 50 boards. Backtracking stops a branch the moment a rule is broken.
Find the next empty cell. Try each digit from 1 to 9 that is not already in its row, column or box. Place it and recurse, and if that fails, erase it and try the next digit.
Find an empty cell, try valid digits, undo on failure. Returning true stops the search at the first solution.
Run the animation above on your own input, or watch the 1:27 video explanation of Sudoku Solver with a dry run and Java code.