Hacker News new | ask | show | jobs
by glenjamin 5561 days ago
This is one of the best explanations on how to write an efficient brute-force sudoku solver with constraint propagation, it's in Python but should be perfectly readable.

http://norvig.com/sudoku.html

1 comments

Well the constraint propagation is quite good. My logic adds one more constraint.

If any of the possible values of a cell is not possible in any of the other conflicting cell than its safe to assume that, that value is the value of this cell.

hope its not confused. check the line 313 to 386 of my code.

The bit you're missing is not the propagation, it's the memoisation. Roughly summarised, norvig's alrogithm is:

1. Construct a 9x9 Array of possibility arrays containing the numbers 1-9. 2. When a possibility array is reduced to 1 element, remove this number from all related arrays (row, column, box). 3. Reduce the arrays to 1 number as specified by the initial puzzle. 4. If, after propagation all possibility arrays have 1 item, the puzzle is solved 5. Otherwise,choose one of the shortest possiblity arrays and try a value, backtrack if this leads to an unsolvable puzzle

The key points are the memoisation of possiblities, and choosing cells with the smallest number of possibilities for the search.