Skip to content
BytePatterns

Fill a Sudoku Grid

HardBacktracking#backtracking#constraint-sets~45m

Problem

A 9 by 9 puzzle grid holds digits as the characters "1" to "9" and "." for empty cells. Fill the empty cells in place so that every row, every column and every 3 by 3 box contains each digit exactly once, and return True. If the givens already clash or no filling exists, return False.

Examples

Input:  grid = ["53..7....", "6..195...", ".98....6.", "8...6...3", "4..8.3..1",
                "7...2...6", ".6....28.", "...419..5", "....8..79"]
Output: True, grid becomes ["534678912", "672195348", "198342567", ...]
Input:  the first row is "11......." and the rest are empty
Output: False
Why:    edge case, the givens already break the row rule, so no search is needed

Hints

0 / 3

Stuck on the idea rather than the code? N-Queens covers it.