Sudoku Solver - Problem
Imagine being handed a partially completed Sudoku puzzle - one of the world's most beloved logic games! Your mission is to write an intelligent program that can automatically solve any valid Sudoku puzzle by filling in the empty cells (marked with '.').
A complete Sudoku solution must follow three sacred rules:
- Row Rule: Each of the digits 1-9 must appear exactly once in each row
- Column Rule: Each of the digits 1-9 must appear exactly once in each column
- Box Rule: Each of the digits 1-9 must appear exactly once in each of the 9 3ร3 sub-boxes
Your algorithm needs to be smart enough to use logical deduction and backtracking to find the unique solution, just like a human solver would - but much faster!
Goal: Transform an incomplete 9ร9 grid into a completely solved Sudoku puzzle.
Input & Output
example_1.py โ Basic Sudoku Puzzle
$
Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
โบ
Output:
[
["5","3","4","6","7","8","9","1","2"],
["6","7","2","1","9","5","3","4","8"],
["1","9","8","3","4","2","5","6","7"],
["8","5","9","7","6","1","4","2","3"],
["4","2","6","8","5","3","7","9","1"],
["7","1","3","9","2","4","8","5","6"],
["9","6","1","5","3","7","2","8","4"],
["2","8","7","4","1","9","6","3","5"],
["3","4","5","2","8","6","1","7","9"]
]
๐ก Note:
This is a typical Sudoku puzzle with a unique solution. The algorithm fills in the missing numbers while ensuring each row, column, and 3x3 box contains all digits 1-9 exactly once.
example_2.py โ Nearly Complete Puzzle
$
Input:
[
["5","3","4","6","7","8","9","1","."],
["6","7","2","1","9","5","3","4","8"],
["1","9","8","3","4","2","5","6","7"],
["8","5","9","7","6","1","4","2","3"],
["4","2","6","8","5","3","7","9","1"],
["7","1","3","9","2","4","8","5","6"],
["9","6","1","5","3","7","2","8","4"],
["2","8","7","4","1","9","6","3","5"],
["3","4","5","2","8","6","1","7","9"]
]
โบ
Output:
[
["5","3","4","6","7","8","9","1","2"],
["6","7","2","1","9","5","3","4","8"],
["1","9","8","3","4","2","5","6","7"],
["8","5","9","7","6","1","4","2","3"],
["4","2","6","8","5","3","7","9","1"],
["7","1","3","9","2","4","8","5","6"],
["9","6","1","5","3","7","2","8","4"],
["2","8","7","4","1","9","6","3","5"],
["3","4","5","2","8","6","1","7","9"]
]
๐ก Note:
This puzzle has only one empty cell. The constraint propagation approach would immediately identify that only '2' can go in position [0][8], making this solve in O(1) time.
example_3.py โ Challenging Puzzle
$
Input:
[
[".",".",".",".",".",".","6","8","."],
[".",".",".",".","4",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".","4",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".",".","."],
[".",".",".",".","2",".",".",".","."],
[".",".",".",".",".",".",".",".","."]
]
โบ
Output:
[
["2","7","4","3","9","1","6","8","5"],
["1","3","6","8","4","5","9","2","7"],
["8","9","5","6","7","2","3","1","4"],
["6","1","8","9","5","3","7","4","2"],
["3","5","9","1","6","7","4","9","8"],
["7","4","2","4","8","9","1","5","6"],
["5","6","1","2","3","4","8","7","9"],
["9","8","3","7","2","6","5","6","1"],
["4","2","7","5","1","8","2","3","3"]
]
๐ก Note:
This sparse puzzle with very few given numbers represents a challenging case where backtracking becomes essential. The MCV heuristic helps by choosing cells with fewer possibilities first.
Constraints
-
board.length == 9 -
board[i].length == 9 -
board[i][j]is a digit1-9or'.' - It is guaranteed that the input board has only one solution
Visualization
Tap to expand
Understanding the Visualization
1
Setup Constraints
Initialize tracking sets for each row, column, and 3ร3 box with digits 1-9, then remove already placed numbers
2
Constraint Propagation
Find cells with only one possible value and fill them immediately - like solving obvious clues first
3
Most Constrained Variable
Among remaining empty cells, choose the one with fewest possible values to branch on
4
Intelligent Backtrack
Try each possibility, update constraints, and recursively solve. Backtrack with state restoration if stuck
Key Takeaway
๐ฏ Key Insight: Combining constraint propagation with the Most Constrained Variable heuristic transforms an exponential search into a highly efficient algorithm that solves most Sudoku puzzles in milliseconds.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code