Your task is to design an algorithm used to create a Sudoku Grid. The generated Sudoku grid should have enough clues (numbers in cells) to be solvable resulting in a unique solution.
Sudoku?
A Sudoku game is number-placement puzzle. The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid (also called “boxes”, “blocks”, or “regions”) contain all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.
Our aim for this challenge is not to generate a Sudoku solver algorithm but instead to create an algorithm to be used by a puzzle setter to produce a well-posed Sudoku grid: a grid with a unique solution. For instance the output of our algorithm could be a grid such as this one:
Did You Know?
Sudoku fanatics have long claimed that the smallest number of starting clues a Sudoku puzzle can contain is 17. There are effectively numerous examples of grids with 17 clues that have a unique solution but we have never found a well-posed grid with only 16 clues. This suggests that the minimum number of clues to provide in a grid is 17.
This key fact might be useful to help you solve this challenge more effectively.
Sudoku Solver Algorithm
Your Sudoku Generator algorithm may need to use a Sudoku Solver Algorithm in order to test whether a generated grid is solvable and to check that it only gives a single solution.
The most common type of Sudoku Solver Algorithm is based on a backtracking algorithm used to investigate all possible solutions of a given grid.
You can find an example of such an algorithm by investigating the code provided in this Python Challenge: Sudoku Solver using a Backtracking Algorithm
Extension Task:
Sudoku puzzles are often given a difficulty level such as “Beginner – Intermediate – Advanced – Expert”.
How could your algorithm be adapted to estimate the difficulty level of a Sudoku grid?
Should different algorithms be used to generate Sudoku grids for a specific difficulty level?
Solution
Our solution is based on 5 steps:
- Generate a full grid of numbers (fully filled in). This step is more complex as it seems as we cannot just randomly generate numbers to fill in the grid. We have to make sure that these numbers are positioned on the grid following the Sudoku rules. To do so will use a sudoku solver algorithm (backtracking algorithm) that we will apply to an empty grid. We will add a random element to this solver algorithm to make sure that a new grid is generated every time we run it.
- From our full grid, we will then remove 1 value at a time.
- Each time a value is removed we will apply a sudoku solver algorithm to see if the grid can still be solved and to count the number of solutions it leads to.
- If the resulting grid only has one solution we can carry on the process from step 2. If not we will have to put the value we took away back in the grid.
- We can repeat the same process (from step 2) several times using a different value each time to try to remove additional numbers, resulting in a more difficult grid to solve. The number of attempts we will use to go through this process will have an impact on the difficulty level of the resulting grid.
Full Python Code
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area