I left a command line version of this running overnight, it ran for about 12 hours or so, generating about 200k Sudokus!
- Code: Select all
...2....48..........3..1.25...41..63.....29.7.4..9....58....731....7....6..12..5. 26 ED=9.2/1.2/1.2 74,941st
..6....7....7..1..9...4...529.4.......1.5.9..7..3..2...8.96...1.1.......6.7..84.. 25 ED=9.3/1.2/1.2 158,163rd
SudokuExplainer checks a Sudoku has a unique solution by (bruteforce) solving the puzzle twice as follows:
solution 1:
- Code: Select all
- solves all singles
- pick 1st cell with least number of candidates, e.g. r1c2
- try lowest to highest value candidate, e.g. r1c2, try 2, 8 then 9
- check grid can still be solved and has one solution
- else restore candidate and try next lowest candidate
- loop until solved
solution 2:
- Code: Select all
- solves all singles
- pick 1st cell with least number of candidates, e.g. r1c2
- try highest to lowest value candidate, e.g. r1c2, try 9, 8 then 2
- check grid can still be solved and has one solution
- else restore candidate and try next highest candidate
- loop until solved
if solution 1 = solution 2, then the solution is unique.
if solution 1 is not equal to solution 2, then the Sudoku has multiple solutions.
SudokuExplainer creates a random solution grid by passing an empty grid to above solver [solution 1] with an index tweak:
random solution grid:
- Code: Select all
- solves all singles
- pick 1st cell with least number of candidates, e.g. r1c1
- try a random offset to pick lowest to highest value candidate, e.g. r1c1, 123456789[random offset]
- check grid can still be solved and has one solution
- else restore candidate and try next lowest candidate
- loop until solved
SudokuExplainer generates a random Sudoku as follows:
- shuffles a list of cells into a random list of cells, e.g. 1-81 into e.g. 21,54,7,32,...
- process list starting from a random index, remove one (no symmetry) or more (symmetrical) given(s), e.g. cell 48 = 0
- check grid can still be solved and has one solution [as above]
- if grid cannot be solved, restore cell and repeat with next from list
- if grid is solved, restart process from list starting from another random index
- repeat above, until list of cells [81] processed with no new grid produced
- rate the grid, if rating not in requested range, repeat all of above
To show the Sudoku is minimal:
- remove a given from the Sudoku, and test if this has a unique solution [as above]
- try all givens, one at a time - if none have a unique solution, then Sudoku is minimal
SudokuExplainer does not check for minimal grid when generating, just for unique solution.
.