/* Kakuru puzzle in Comet. http://en.wikipedia.org/wiki/Kakuro """ The object of the puzzle is to insert a digit from 1 to 9 inclusive into each white cell such that the sum of the numbers in each entry matches the clue associated with it and that no digit is duplicated in any entry. It is that lack of duplication that makes creating Kakuro puzzles with unique solutions possible, and which means solving a Kakuro puzzle involves investigating combinations more, compared to Sudoku in which the focus is on permutations. There is an unwritten rule for making Kakuro puzzles that each clue must have at least two numbers that add up to it. This is because including one number is mathematically trivial when solving Kakuro puzzles; one can simply disregard the number entirely and subtract it from the clue it indicates. """ This model solves the problem at the Wikipedia page. For a larger picture, see http://en.wikipedia.org/wiki/File:Kakuro_black_box.svg The solution: 9 7 0 0 8 7 9 8 9 0 8 9 5 7 6 8 5 9 7 0 0 0 6 1 0 2 6 0 0 0 4 6 1 3 2 8 9 3 1 0 1 4 3 1 2 0 0 2 1 Also, see the following related models: - http://www.hakank.org/comet/kenken.co - http://www.hakank.org/comet/kenken2.co - http://www.hakank.org/comet/killer_suduko.co This Comet model was created by Hakan Kjellerstrand (hakank@bonetmail.com) Also, see my Comet page: http://www.hakank.org/comet */ import cotfd; int t0 = System.getCPUTime(); int n = 7; range R = 0..9; tuple cell { int r; int c; } tuple P { set{cell} cells; int res; } // // state the problem // int num_p = 24; // number of segments P problem[1..num_p] = [ P({cell(1,1),cell(1,2)}, 16), P({cell(1,5),cell(1,6),cell(1,7)}, 24), P({cell(2,1),cell(2,2)}, 17), P({cell(2,4),cell(2,5),cell(2,6),cell(2,7)},29), P({cell(3,1),cell(3,2),cell(3,3),cell(3,4),cell(3,5)},35), P({cell(4,2),cell(4,3)},7), P({cell(4,5),cell(4,6)},8), P({cell(5,3),cell(5,4),cell(5,5),cell(5,6),cell(5,7)},16), P({cell(6,1),cell(6,2),cell(6,3),cell(6,4)},21), P({cell(6,6),cell(6,7)},5), P({cell(7,1),cell(7,2),cell(7,3)},6), P({cell(7,6),cell(7,7)},3), P({cell(1,1),cell(2,1),cell(3,1)},23), P({cell(1,2),cell(2,2),cell(3,2),cell(4,2)},30), P({cell(1,5),cell(2,5),cell(3,5),cell(4,5),cell(5,5)},27), P({cell(1,6),cell(2,6)},12), P({cell(1,7),cell(2,7)},16), P({cell(2,4),cell(3,4)},17), P({cell(3,3),cell(4,3),cell(5,3),cell(6,3),cell(7,3)},15), P({cell(4,6),cell(5,6),cell(6,6),cell(7,6)},12), P({cell(5,4),cell(6,4)},7), P({cell(5,7),cell(6,7),cell(7,7)},7), P({cell(6,1),cell(7,1)},11), P({cell(6,2),cell(7,2)},10) ]; // // the blanks, coded as 0 in the matrix // int num_blanks = 13; cell blanks[1..num_blanks] = [ cell(1,3), cell(1,4), cell(2,3), cell(3,6), cell(3,7), cell(4,1), cell(4,4),cell(4,7), cell(5,1), cell(5,2), cell(6,5), cell(7,4), cell(7,5) ]; Solver m(); var{int} x[1..n, 1..n](m, R); function void calc(Solver m, set{cell} cc, var{int}[,] x, int res, int n) { // restrict the range to 1..n forall(i in cc) m.post(x[i.r, i.c] >= 1); // sum the numbers m.post(sum(i in cc) x[i.r, i.c] == res ); } // end calc Integer num_solutions(0); exploreall { // fix the blanks with 0 forall(i in 1..num_blanks) m.post(x[blanks[i].r,blanks[i].c]==0); forall(i in 1..num_p) { // sum the segment calc(m, problem[i].cells, x, problem[i].res, n); // all numbers in this segment must be distinct m.post(alldifferent(all(i in problem[i].cells) x[i.r, i.c])); } } using { labelFF(m); num_solutions := num_solutions + 1; forall(i in 1..n) { forall(j in 1..n) { int c = x[i,j]; if (c != 0) { cout << c; } else { cout << "."; } cout << " "; } 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;