Sudoku solver: Difference between revisions

From Opasnet
Jump to navigation Jump to search
No edit summary
Line 128: Line 128:
This table will be expanded by fillna to be a 9*9*9 array (formatted as data.frame). As default, each hypothesis is assumed to be true unless shown otherwise.
This table will be expanded by fillna to be a 9*9*9 array (formatted as data.frame). As default, each hypothesis is assumed to be true unless shown otherwise.


<t2b name="Hypotheses" index="SudRow,SudCol,SudNum" obs="Hypothesis" unit="Boolean">
<t2b name="Hypotheses" index="SudRow,SudCol,Hypothesis" obs="Result" unit="Boolean">
||1|TRUE
||1|TRUE
||2|TRUE
||2|TRUE

Revision as of 18:11, 29 April 2013



Question

How to describe a sudoku and the sudoku rules in Opasnet so that it can be solved automatically?

Answer

You need the following tables.

Hypotheses
Row Column Result Description
All All 1,2,3,4,5,6,7,8,9 For all row and column locations it applies that the plausible hypotheses are a single integer between 1 and 9 (unless more information is available).


Area descriptions
Row Column Area
1 1 A
1 2 A
1 3 A
1 4 B
2 1 A
4 1 D
9 9 I


Rules of exclusion when comparing two cells.
Property1 Condition1 Property2 Condition2 Rule Description
Row Same Column Different Same integer not allowed Two cells with the same row and different column are not allowed to have the same integer.
Row Different Column Same Same integer not allowed Two cells with the different row and same column are not allowed to have the same integer.
Area Same Column Different Same integer not allowed Two cells with the same area and different column are not allowed to have the same integer.
Area Same Row Different Same integer not allowed Two cells with the same area and different row are not allowed to have the same integer.
The sudoku data (this example is "the most difficult sudoku in the world")
Row Column
1 2 3 4 5 6 7 8 9
1 8
2 3 6
3 7 9 2
4 5 7
5 4 5 7
6 1 3
7 1 6 8
8 8 5 1
9 9 4

Procedure

  1. Expand the "All" from the Hypothesis table to create a row for the hypothesis of each cell.
  2. Take the sudoku data table and replace hypotheses with data, if available.
  3. Compare two cells in the sudoku. Make a for loop the first cell: for(i in 1:nrow(hypothesis)).
    1. Make another for loop for the second cell: for(j in (i+1):nrow(hypothesis)).
      1. Make a third loop for all rules: for(k in 1:nrow(rules)).
        1. Test for the rule with the pair of cells, creating a set of plausible hypothesis for one cell conditional on the other cell.
        2. If a set is empty, the condition is implausible; remove the condition and thus that hypothesis from the other cell.
        3. Take the union of plausible hypothesis (which then covers all plausible hypotheses unconditionally.
        4. Do the same comparison for the other cell conditional on the first one.
  4. If a unique solution was not found and if the current set of hypotheses is not the same as the previous set, save the current set as "previous set" and go to number 2.
  5. Calculate the number of different solutions still plausible and print it.
  6. If the number is smaller than 100, print also the solutions.

Rationale

Data

This table will be expanded by fillna to be a 9*9*9 array (formatted as data.frame). As default, each hypothesis is assumed to be true unless shown otherwise.

Hypotheses(Boolean)
ObsSudRowSudColHypothesisResult
11TRUE
22TRUE
33TRUE
44TRUE
55TRUE
66TRUE
77TRUE
88TRUE
99TRUE
101TRUE
112TRUE
123TRUE
134TRUE
145TRUE
156TRUE
167TRUE
178TRUE
189TRUE
191TRUE
202TRUE
213TRUE
224TRUE
235TRUE
246TRUE
257TRUE
268TRUE
279TRUE

Formula used: Rowarea <- ceiling(SudRow/3), Colarea <- ceiling(SudCol/3)

Areas(-)
ObsRowareaColareaSudAreaResult
1111
2122
3133
4214
5225
6236
7317
8328
9339

Formula

+ Show code

See also

Keywords

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>