|
The SuDoku Solver's algorithm only uses logical arguments to solve the puzzles. It does not use 'trial and error' methods. For anybody interested in an explanation of the algorithm it employs to solve your SuDoku then please read on.
The Solver stores two grids (as arrays), one is the actual SuDoku grid itself, and the second contains all the 'possible values' for each square. Initially all values (1-9) would be possible for all squares of course. The algorithm then tries a number of steps to solve the puzzle, stopping when it either solves the SuDoku or all the following steps fail to find any new values.
Before moving onto how the SuDoku Solver algorithm finds unknown squares lets first see what happens when the algorithm adds a new value to the grid, either from the initial values entered by the user or from
using the solve methods below. When the solver knows the value of a square it will adjust the 'possible values' grid for all other relevant squares.
For example (fig 1), if the value '1' is placed in grid position column 5, row 1 then all other squares in column 5, row 1 and the 3x3 block this square belongs to can no longer possibly contain the value '1'. So the solver updates the 'possible values' grid with this information.

Fig 1. The value '1' is placed at (5,1), all the yellow squares can now not possibly contain '1'
Step 1: Only one valid value for a square
SuDoku Solver checks to see if any of the squares have only one possible value.
If any are found then the value is entered for this square as mentioned above and Step 1 is repeated until all remaining unsolved squares contain multiple possible values (or the SuDoku is completely solved!)

Fig 2. The value '8' is placed at (5,5) as no other value is possible!
Step 2: Only one valid square for a value in a row / column / block
If the above method fails to solve the puzzle then this next method is tested. Each row / line / block is checked to see if any of the values (1-9) that have yet to be found for that row / column / block can only be placed in a single square. If so that value is entered and the algorithm returns to Step 1.

Fig 3. The column 5 requires a value '1', although each square has a large number of
possible values only a single square (shown in yellow) can possibly store the value '1'.
If we still have not solved the puzzle at this point the solver moves onto the following methods which do not directly try to find the value for a square but rather reduce the possible values for a square in the hope this leads to Step 1 or 2 being able to then solve another square.
Step 3: Value must be here, so can not be there!
This one is a bit more complex to explain, if you look in a 3x3 block and all the places a certain value can be put fall on
the same column / row then the value must lie within these squares so can not possibly lie in the other six squares in this
column / row. The argument can also be used the opposite way around, looking at lines where all allowed locations are within the
same 3x3 block. The following images may make this clearer.
Fig 4. The values '7', '8' and '9' must lie in the three yellow squares. Because these
squares are all part of column / row 5 then the three values can not possibly lie within the blue coloured squares.
Step 4: Multi-sets stick together
Another more abstract method, if a column / row / block has say two values which both only have two allowed possible squares, and these two
squares are the same for both values then this pair of values must be in these two squares. All other values therefore can not
be placed in these squares (if any others exist). The above is repeated for sets of three, four and five values too.

Fig 5. The two yellow squares could possibly be any of the values 5-9. But because no other squares in this block can be '5' or '6'
then the rule above states these two squares must be '5' and '6'. Therefore the two squares can not possibly be 7,8 or 9.
Currently if the algorithm has failed to solve the puzzle using the above steps then this solver will have taken the puzzle as far as it can. We are working
on improvements to this algorithm which will include new methods to improve the algorithm and help solve the SuDoku in the next version.
|