Brute force sudoku cracker
Gregory Bond
gnb at itga.com.au
Mon Sep 19 01:59:23 EDT 2005
My current solver does 1 level of backtracking (i.e. constant space, and
bounded time) only, and it has been able to solve every puzzle I've
thrown at it. It's based on the usual logic and book-keeping for the
most part. (It also explains how it comes up with each answer step as
it goes, which is handy...)
Once it gets to the stage where it needs to guess, it arranges all the
unknowns then tries them in some arbitary order. It saves the state,
applies the guess (square x,y is N) and then re-applies all the logic
rules. There are 3 possible outcomes from here:
- We've solved it, which is good (this happens surprisingly often....)
- We can't solve it using this guess (so ignore this possibility,
restore the state & try the next guess)
- The Resulting puzzle is badly formed / inconsistant (represented by
a python exception, naturally!) In this case, we know the guessed
square/number is not valid, so we backtrack (restore the state before we
made this guess), remove that possibility (x,y is N) and then apply all
the logic rules again. Often times, this is enough to solve, but it
usually progreses things a little even if it doesn't solve it.
I've not yet found a (9x9) puzzle that this cannot solve. The downside
is that it cannot detect cases where there are multiple solutions.
More information about the Python-list
mailing list