Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate Sudoku boards with unique solutions

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?

like image 582
guilin 桂林 Avatar asked Aug 03 '11 09:08

guilin 桂林


People also ask

Is there a unique solution for every Sudoku puzzle?

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.

How are Sudoku boards created?

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.

How many possible unique Sudoku solutions are there?

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.

How do you create Sudoku puzzles?

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.


3 Answers

Here is the way my own SuDoKu program does it:


  1. Start with a complete, valid board (filled with 81 numbers).

  2. Make a list of all 81 cell positions and shuffle it randomly.

  3. As long as the list is not empty, take the next position from the list and remove the number from the related cell.

  4. 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.

  5. If the current board has still just one solution, goto step 3) and repeat.

  6. 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

  7. 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":


  1. Start with an empty board.

  2. 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).

  3. 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.

  4. Repeat until the board is completely filled with numbers.

like image 118
Doc Brown Avatar answered Nov 13 '22 18:11

Doc Brown


Easy:

  1. Find all solutions with an efficient backtracking algorithm.
  2. If there is just one solution, you are done. Otherwise if you have more than one solution, find a position at which most of the solutions differ. Add the number at this position.
  3. Go to 1.

I doubt you can find a solution that would be much faster than this.

like image 44
Tomas Avatar answered Nov 13 '22 20:11

Tomas


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.

like image 32
rossum Avatar answered Nov 13 '22 19:11

rossum