Download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 
  Killer sudoku in Comet.
 
  """
  Killer sudoku (also killer su doku, sumdoku, sum doku, addoku, or
  samunamupure) is a puzzle that combines elements of sudoku and kakuro.
  Despite the name, the simpler killer sudokus can be easier to solve
  than regular sudokus, depending on the solver's skill at mental arithmetic;
  the hardest ones, however, can take hours to crack.
 
  ...
 
  The objective is to fill the grid with numbers from 1 to 9 in a way that
  the following conditions are met:
 
    * Each row, column, and nonet contains each number exactly once.
    * The sum of all numbers in a cage must match the small number printed
      in its corner.
    * No number appears more than once in a cage. (This is the standard rule
      for killer sudokus, and implies that no cage can include more
      than 9 cells.)
 
  In 'Killer X', an additional rule is that each of the long diagonals
  contains each number once.
  """
 
  Here we solve the problem from the Wikipedia page, also shown here
 
  Note, this model is based on the generalized KenKen model:
  Killer Sudoku is simpler in that the only mathematical operation is
  summation.
 
  The output is:
    2 1 5 6 4 7 3 9 8
    3 6 8 9 5 2 1 7 4
    7 9 4 3 8 1 6 5 2
    5 8 6 2 7 4 9 3 1
    1 4 2 5 9 3 8 6 7
    9 7 3 8 1 6 4 2 5
    8 2 1 7 3 9 5 4 6
    6 5 9 4 2 8 7 1 3
    4 3 7 1 6 5 2 8 9
 
    num_solutions: 1
    time:      12
    #choices = 56
    #fail    = 136
    #propag  = 3013
     
 
  This Comet model was created by Hakan Kjellerstrand (hakank@gmail.com)
  Also, see my Comet page: http://www.hakank.org/comet
 
 */
 
// Licenced under CC-BY-4.0 : http://creativecommons.org/licenses/by/4.0/
 
import cotfd;
 
int t0 = System.getCPUTime();
 
int n = 9;
range R = 1..n;
 
 
tuple cell {
  int r; // row
  int c; // column
}
 
tuple P {
  set{cell} cells; // cells
  int res; // result
}
 
//
// state the problem (without the operation)
//
int num_p = 29; // number of cages
P problem[1..num_p] =
  [
   P({cell(1,1), cell(1,2)}, 3),
   P({cell(1,3), cell(1,4), cell(1,5)}, 15),
   P({cell(1,6), cell(2,5), cell(2,6), cell(3,5)}, 22),
   P({cell(1,7), cell(2,7)}, 4),
   P({cell(1,8), cell(2,8)}, 16),
   P({cell(1,9), cell(2,9), cell(3,9), cell(4,9)}, 15),
   P({cell(2,1), cell(2,2), cell(3,1), cell(3,2)}, 25),
   P({cell(2,3), cell(2,4)}, 17),
   P({cell(3,3), cell(3,4), cell(4,4)}, 9),
   P({cell(3,6), cell(4,6),cell(5,6)}, 8),
   P({cell(3,7), cell(3,8),cell(4,7)}, 20),
   P({cell(4,1), cell(5,1)},6),
   P({cell(4,2), cell(4,3)},14),
   P({cell(4,5), cell(5,5),cell(6,5)},17),
   P({cell(4,8), cell(5,7),cell(5,8)},17),
   P({cell(5,2), cell(5,3),cell(6,2)},13),
   P({cell(5,4), cell(6,4),cell(7,4)},20),
   P({cell(5,9), cell(6,9)}, 12),
   P({cell(6,1), cell(7,1),cell(8,1),cell(9,1)},27),
   P({cell(6,3), cell(7,2),cell(7,3)},6),
   P({cell(6,6), cell(7,6), cell(7,7)}, 20),
   P({cell(6,7), cell(6,8)},6),
   P({cell(7,5), cell(8,4),cell(8,5),cell(9,4)},10),
   P({cell(7,8), cell(7,9),cell(8,8),cell(8,9)},14),
   P({cell(8,2), cell(9,2)}, 8),
   P({cell(8,3), cell(9,3)},16),
   P({cell(8,6), cell(8,7)},15),
   P({cell(9,5), cell(9,6),cell(9,7)},13),
   P({cell(9,8), cell(9,9)},17)
 
   ];
 
Solver<CP> m();
 
var<CP>{int} x[1..n, 1..n](m, R);
 
//
// assumption: only the segments with 2 cells can be minus or div.
//
function void calc(Solver<CP> m, set{cell} cc, var<CP>{int}[,] x, int res) {
  m.post(sum(i in cc) x[i.r, i.c] == res);
}
 
 
Integer num_solutions(0);
 
exploreall<m> {
 
  // all rows, columns, and nonets must be unique
  forall(i in R)
      m.post(alldifferent(all(j in R) x[i,j]));
 
  forall(j in R)
    m.post(alldifferent(all(i in R) x[i,j]));
 
  forall(i in 0..2,j in 0..2) {
    m.post(alldifferent(all(r in i*3+1..i*3+3,c in j*3+1..j*3+3)  x[r,c]));
  }
   
 
  // solve the cages
  forall(i in 1..num_p) {
    calc(m, problem[i].cells, x, problem[i].res);
  }
 
 
} using {
       
  // label(m);
  forall(i in 1..n, j in 1..n : !x[i,j].bound())  {
    tryall<m>(v in 1..n  : x[i,j].memberOf(v))
      label(x[i,j], v);
  }
 
  num_solutions := num_solutions + 1;
       
  forall(i in 1..n) {
    forall(j in 1..n) {
      cout << x[i,j] << " ";
    }
    cout << endl;
  }
 
  cout << endl;
}
 
cout << "\nnum_solutions: " << num_solutions << endl;
     
int t1 = System.getCPUTime();
cout << "time:      " << (t1-t0) << endl;
cout << "#choices = " << m.getNChoice() << endl;
cout << "#fail    = " << m.getNFail() << endl;
cout << "#propag  = " << m.getNPropag() << endl;