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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * OscaR is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *  
 * OscaR is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License  for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public License along with OscaR.
 ******************************************************************************/
package oscar.examples.cp.hakank
 
import oscar.cp.modeling._
 
import oscar.cp.core._
import scala.io.Source._
import scala.math._
 
/**
 *
 * """
 * 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
 *
 * 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
 *
 *
 *  @author Hakan Kjellerstrand hakank@gmail.com
 *
 */
 
object KillerSudoku {
 
 
  /**
   * Ensure that the sum of the segments
   * in cc == res
   *
   */
  def calc(cp: CPSolver,
           cc: Array[Int],
           x: Array[Array[CPIntVar]],
           res: Int) {
 
    val len = (cc.length / 2).toInt
 
    // sum the numbers
    cp.add(sum(for{i <- 0 until len} yield x(cc(i*2)-1)(cc(i*2+1)-1)) == res)
  }
   
  def main(args: Array[String]) {
 
    val cp = CPSolver()
 
    //
    // data
    //
 
    // size of matrix
    val cell_size = 3
    val CELLS = 0 until cell_size
    val n = cell_size*cell_size
    val RANGE = 0 until n
 
    // For a better view of the problem, see
 
    // hints
    //  sum, the hints
    // Note: this is 1-based
    val problem = Array(Array( 31,11,2),
                        Array(151,31,4, 1,5),
                        Array(221,62,5, 2,6, 3,5),
                        Array(4,   1,72,7),
                        Array(161,82,8),
                        Array(151,92,9, 3,9, 4,9),
                        Array(252,12,2, 3,1, 3,2),
                        Array(172,32,4),
                        Array( 93,33,4, 4,4),
                        Array( 83,64,6, 5,6),
                        Array(203,73,8, 4,7),
                        Array( 64,15,1),
                        Array(144,24,3),
                        Array(174,55,5, 6,5),
                        Array(174,85,7, 5,8),
                        Array(135,25,3, 6,2),
                        Array(205,46,4, 7,4),
                        Array(125,96,9),
                        Array(276,17,1, 8,1, 9,1),
                        Array( 66,37,2, 7,3),
                        Array(206,67,6, 7,7),
                        Array( 66,76,8),
                        Array(107,58,4, 8,5, 9,4),
                        Array(147,87,9, 8,8, 8,9),
                        Array( 88,29,2),
                        Array(168,39,3),
                        Array(158,68,7),
                        Array(139,59,6, 9,7),
                        Array(179,89,9))
 
 
    val num_p = problem.length // Number of segments
    println("num_p: " + num_p)
 
    //
    // Decision variables
    //
    val x = Array.fill(n,n)(CPIntVar(0 to 9)(cp))
    val x_flat = x.flatten
 
    //
    // constraints
    //
    var numSols = 0
 
    cp.solve subjectTo {
 
      // rows and columns
      for(i <- RANGE) {
        cp.add(allDifferent( Array.tabulate(n)(j=> x(i)(j))), Strong)
        cp.add(allDifferent( Array.tabulate(n)(j=> x(j)(i))), Strong)
      }
       
      // blocks
      for(i <- CELLS; j <- CELLS) {
        cp.add(allDifferent(  (for{ r <- i*cell_size until i*cell_size+cell_size;
                                    c <- j*cell_size until j*cell_size+cell_size
              } yield x(r)(c)).toArray), Strong)
      }
       
      for(i <- 0 until num_p) {
        val segment = problem(i)
 
        // Remove the sum from the segment
        val s2 = for(i<-1 until segment.length) yield segment(i)                                                 
        // sum this segment
        calc(cp, s2, x, segment(0))
 
        // all numbers in this segment must be distinct
        val len = segment.length / 2
        cp.add( allDifferent(for(j <- 0 until len) yield x(s2(j * 2) - 1)(s2(j * 2 + 1) - 1)))
 
      }
 
    } search {
        
      binaryFirstFail(x_flat)
    } onSolution {
       
      for(i <- RANGE) {
        println(x(i).mkString(""))
      }
      println()
 
      numSols += 1
 
   }
   println(cp.start())
 
  }
 
}