The idea here is that you associate to
each position on the board those values which that place can
take - or
cannot take.
If it is in the same row, column, or square as a
given (or calculated) value then that value is not possible. One
can be a little clever than this by considering what values it
cannot be given the arrangements
in the other squares. Then one can start being much
much cleverer: there are, in fact, all sorts of clever reasons why one
can
work out the value of a square given a board configuration.
However, don't get sucked in to this black hole! It
can be a lot of fun finding new clever special case rules but you must
ask yourself "how many cases will this actually catch?" Look on
the
sudoku sites for some
basic situations to catch ... if you find yourself going beyond these
you are probably going too far!
Your aim is at this stage is to get the obvious constrains implemented
and --
very importantly -- to
discover whether a board configuration is not possible as soon as
possible. While working out the constraints three thing can
happen:
(a) you can actually complete the board
and get a solution
(b) you can discover that the board
configuration is not possible
(c) (most likely in hard puzzles) you
obtain a partially filled in
configuration with constraints.
In the first case you are done! In the second case you want to
continue to apply the constraints until there is nothing new to
discover. In the last case you have to resort to guessing
possible values
...
B. The last stage is a search phase.
There are two basic methods: depth-first or breadth-first
search. Depth-first methods are better as there is no
sharing of board configurations and this search method require
considerably less storage. The basic idea is that you
keep
an agenda (=list) of partial solutions and you repeatedly develop
the "best" or "most promising" partial solution on the agenda.
The development involves working out the constraints on the board (if
the configuration is impossible move onto the next item on the agenda)
once one has updated the constraints replace the board configuration by
a list of possible configurations where you have guessed the possible
values of a promising position (usually one with the least number of
possibilities). These guesses go back on the agenda.
How big is the search? In most cases the search tree (if you do
the constraints well!) will only be two or three steps. For
a puzzle to be manageable by hand this is a requirement! In
general, a rough estimate of the maximal depth of the tree (number of
guesses you make) is about 10 (you will start with about 20 places
given and by the time thirty have been filled in the constraints will
force an answer). That said there are going to be really
bad cases ...
A measure of the difficulty of the puzzle is the number of guesses
you have to make: so you can actually grade the puzzles
yourself!
Haskell, as provided by Hugs, is not
wildly efficient and certainly you are not expected to get into bit
manipulating tricks for the problem representation (which if you wanted
it to fly would be necessary) ... so while the program need not be
instantaneous on a 9 by 9 sudoku
board you should be able to get the solution in a couple of
seconds!