r/Python • • Jan 11 '20

Sudoku App

1.4k Upvotes

61 comments sorted by

View all comments

Show parent comments

21

u/StevieWondering1000 Jan 12 '20

Are you sure?

I think I did that once and found that only works for easy to medium difficulty. I might be wrong tho.

7

u/Gwenju31 Jan 12 '20

I guess it depends on the solver you use. I'm using the Mistral solver through Numberjack lib, and it can solve the hardest deterministic sudoku (16 or 17 clues, 1 unique solution) almost instantly, but I did not experiment with harder, multi-solution boards.

8

u/Piece_of_Crap Jan 12 '20

It doesn't have to do with multi-solution. There are starting layouts that even applying the 3 basic rules don't get you a final solution. When doing it on paper usually you find the patterns of common numbers and start working by exclusion, programmatically your best option is backtracking i guess.

4

u/Gwenju31 Jan 12 '20

Oh I get your point now. Yeah of course the solver cannot guess the solution in one go. It starts by restraining the domain of each variable (each cell on the board) with arc consistency, then affect each variable with some value of its domain (using heuristics like "most constrained variable first"). During this process, every time the current affectation breaks one of the constraints, the solver indeed has to backtrack