How do you generate a Sudoku board with a unique solution? What I thought was to initialize a random board and then remove some numbers. But my question is how do I maintain the uniqueness of a solution?
A properly formulated Sudoku puzzle has a unique solution. One can assume that a given puzzle actually is properly formulated, and use that in the reasoning, to exclude branches that would not lead to a unique solution.
Most puzzle generators use random assignment of numbers to cells (starting from a blank sudoku board) until the puzzle can be solved to produce one unique board. Puzzle solvers typically use one of two methods: backtracking (or brute force), and logical deduction similar to that used by a human.
There are exactly 6, 670, 903, 752, 021, 072, 936, 960 possible solutions to Sudoku (about 10^21) . That's far more than can be checked in a reasonable period of time. But as luck would have it, it's not necessary to check them all. Various symmetry arguments prove that many of these grids are equivalent.
Three squares should be in the top row, three in the middle row, and three in the bottom row, with three evenly spaced lines across the square and three evenly spaced lines down the square. If you want to ensure your lines are straight, use a ruler. Ensure it's right by comparing it to existing sudoku puzzles.
Here is the way my own SuDoKu program does it:
Start with a complete, valid board (filled with 81 numbers).
Make a list of all 81 cell positions and shuffle it randomly.
As long as the list is not empty, take the next position from the list and remove the number from the related cell.
Test uniqueness using a fast backtracking solver. My solver is - in theory - able to count all solutions, but for testing uniqueness, it will stop immediately when it finds more than one solution.
If the current board has still just one solution, goto step 3) and repeat.
If the current board has more than one solution, undo the last removal (step 3), and continue step 3 with the next position from the list
Stop when you have tested all 81 positions.
This gives you not only unique boards, but boards where you cannot remove any more numbers without destroying the uniqueness of the solution.
Of course, this is only the second half of the algorithm. The first half is to find a complete valid board first (randomly filled!) It works very similar, but "in the other direction":
Start with an empty board.
Add a random number at one of the free cells (the cell is chosen randomly, and the number is chosen randomly from the list of numbers valid for this cell according to the SuDoKu rules).
Use the backtracking solver to check if the current board has at least one valid solution. If not, undo step 2 and repeat with another number and cell. Note that this step might produce full valid boards on its own, but those are in no way random.
Repeat until the board is completely filled with numbers.
Easy:
I doubt you can find a solution that would be much faster than this.
You can cheat. Start with an existing Sudoku board that can be solved then fiddle with it.
You can swap any row of three 3x3 blocks with any other row. You can swap any column of three 3x3 blocks with another column. Within each block row or block column you can swap single rows and single columns. Finally you can permute the numbers so there are different numbers in the filled positions as long as the permutation is consistent across the whole board.
None of these changes will make a solvable board unsolvable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With