the 81 cells of a Sudoku grid are indexed from 1 to 81 in row order, they can also be indexed with a Row Col Box coordinate system, the conversion from one system to the other give this equivalence:
- Code: Select all
1 2 3 4 5
(1 1 1) (1 2 1) (1 3 1) (1 4 2) (1 5 2)
10 11 12 13 14
(2 1 1) (2 2 1) (2 3 1) (2 4 2) (2 5 2)
two cells are connected if they have at least one of the R C or B coordinates equal, otherwise they are disconnected
a template is a set of nine cells pairwise disconnected
n templates are compatibles if their sets of cells are pairwise disjoint
a puzzle solution is composed of nine compatible templates, one for each of the nine values
it is relatively fast to compute the templates:
- Code: Select all
for i in B1(9) j in B2(6) k in B3(3)
l in B4(6) m in B5(4) n in B6(2)
o in B7(3) p in B8(2) q in B9(1) gives the 46656 (i j k l m n o p q) templates
the algorithm is four cases in a loop
- Code: Select all
(loop
(case key(1234)
(1)
(2)
(3)
(4)
)
)
two variables are used: key to select the case and combTP from 2 to 8 to select the number of templates to check for compatibility
before the loop begins
-the puzzle is initialized
-a scan for Singles is done, if needed the grid is updated
-the key is set to 1
case 1:
a pattern for a value is the set of cells for that value
a valid template for a value is a template with the pattern of the value as a subset and compatible with the others patterns
candidates for a value is the set of cells with this value as candidate
for each value:
-the pattern is computed
-the valid templates are computed
-the candidates are computed
the key is set to 2
combTP is set to 2
case 2:
for each value the intersection of all its valid templates is done to see if some cells are common to all
if so
-the cells are set to this value and a scan for Singles is done
--if the grid is solved the loop is exited if not the key is set to 1
if not
-the key is set to 3
case 3:
for each value the set-difference between the set of the candidate cells and the set of the cells of all the valid templates is done to see if some candidate cells are not in any templates
if so
-the value is eliminated from these cells and a scan for Singles is done
--if the grid is solved the loop is exited
--if not if some singles have been set the key is set to 1
--if not the candidates are updated the key is set to 4
if not the key is set to 4
case 4:
the valid templates are tested for compatibility by group of combTP in the range 2 to 8
to be compatible a template must be a least in one of the combinations of its group
for example: there are m templates for 1 n for 2, a template for 1 to be valid must be compatible with at least one of the templates for 2
if some are found incompatibles they are removed from the valid templates the key is set to 2 combTP is set to 2
if not combTP is incremented the key is set to 4
of course there are practicalities issues and some optimization to do to manage the combinatoric explosion that happen sometimes.
as a example of its working the output of the algorithm for PG 375
1..2..3.4.3..4..5...6..3..17..8..1...1..7..8...9..4..75..4..6...9..2..1.3.1..8..2 10.4/10.4/9.9
Hidden Text: Show