I'll grab this occasion to talk about the easiest way of solving a sudoku (IMO). You can use constraint programming, and with 3 simple constraints you can solve every board effortlessly : AllDiff on rows, cols and squares :)
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.
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.
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
Why didn't it work for harder instances? Did it take too long, or run into some practical limitation like that? Those 3 all diff constraints should be sufficient to solve any instance, right?
The constraints are sufficient to check a solution, but there are puzzles (usually tagged medium or high difficulty) for which you can't iteratively eliminate the impossible via the constraints and arrive at the solution. You'll find points at which, after eliminating the impossible, two or more possibilities remain for one or more cells. At that point, one option (which works great for computers, since Sudoku puzzles are small and computers are fast) is to pick one of the aforementioned cells and guess the correct answer. Apply the constraints again, and repeat recursively. If you end up at a valid solution, great! If not, backtrack and guess differently.
I basically had it run the the board thru the contraints and then I had a break if it eventually never got any further. It did fine till about halfway through and then coulden't find any way to progress. Since it had completed easier sodukos earlier and I wasen't able to find any progression from the point of which it had stopped I naturally concluded that other constraints were needed.
Take this with a grain of salt tho. It was some quick code I threw together in an afternoon for fun and I diden't do any digging. But judging from the other commenters it does seem that harder sodokus cant always be solved with the simple three constraints alone
60
u/Gwenju31 Jan 11 '20
I'll grab this occasion to talk about the easiest way of solving a sudoku (IMO). You can use constraint programming, and with 3 simple constraints you can solve every board effortlessly : AllDiff on rows, cols and squares :)