Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

May create sudoku boards with multiple solutions #6

Open
Gennadiyev opened this issue Dec 9, 2022 · 4 comments
Open

May create sudoku boards with multiple solutions #6

Gennadiyev opened this issue Dec 9, 2022 · 4 comments

Comments

@Gennadiyev
Copy link

For a sudoku board to be valid, there should be exactly ONE solution to an unsolved board. That means the difficulty parameter (in float) could tamper the legitimacy of a sudoku problem.

There are quite a lot of solutions for creating sudoku boards with only one solution.

@alexisflesch
Copy link
Contributor

alexisflesch commented Jul 23, 2024

I think a better way to create puzzles would be to start with a full grid, but instead of randomly removing a certain amount of cells, randomly remove cells one by one until the puzzle is no more uniquely solvable. Then, we could remove between 0 and 100% of the unnecessary cells.

I just made a pull request with a way of knowing whether a grid has a unique solution or not. If someone wants to go forward from this, it should not be too hard.

By the way, thanks a lot @jeffsieu for your work, it gave me a nice way to generate pdfs of Sudokus of various sizes and difficulty !

@ChronoDK
Copy link

How do I force it to generate a sudoku with only one solution? With the latest version I can check for multiple solutions, but how can I make it not generate those bad boards at all?

@alexisflesch
Copy link
Contributor

The algorithm used to create boards goes as follow:

  1. Create a valid board
  2. Randomly remove some percentage of the values

So the short answer is : you can't. What you can do though is to use a quick and dirty approach:

  1. Create a valid board
  2. Check whether it has a unique solution:
    • if not, go back to 1.
    • otherwise you're good to go.

What should be done is to write a function that goes like this:

  1. Create a valid board
  2. Remove (randomly) values one by one until the board is no more uniquely solvable (try every remaining value to be sure that it is as empty as can be)
  3. Remove some percentage of the values found in step 2 to create a uniquely solvable grid with given difficulty.

@ChronoDK
Copy link

Thanks, I did the quick and dirty. It's fine for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants