/* KenKen in Comet. This version is a more general solution than http://www.hakank.org/comet/kenken.co (which compare). http://en.wikipedia.org/wiki/KenKen """ KenKen or KEN-KEN is a style of arithmetic and logical puzzle sharing several characteristics with sudoku. The name comes from Japanese and is translated as "square wisdom" or "cleverness squared". ... The objective is to fill the grid in with the digits 1 through 6 such that: * Each row contains exactly one of each digit * Each column contains exactly one of each digit * Each bold-outlined group of cells is a cage containing digits which achieve the specified result using the specified mathematical operation: addition (+), subtraction (-), multiplication (x), and division (÷). (Unlike in Killer sudoku, digits may repeat within a group.) ... More complex KenKen problems are formed using the principles described above but omitting the symbols +, -, x and ÷, thus leaving them as yet another unknown to be determined. """ The solution is: 5 6 3 4 1 2 6 1 4 5 2 3 4 5 2 3 6 1 3 4 1 2 5 6 2 3 6 1 4 5 1 2 5 6 3 4 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 = 6; range R = 1..6; tuple cell { int r; int c; } tuple P { set{cell} c; int res; } // // state the problem (without the operation) // // For a better view of the problem, see // http://en.wikipedia.org/wiki/File:KenKenProblem.svg int num_p = 15; // number of segments P problem[1..num_p] = [ P({cell(1,1), cell(2,1)}, 11), P({cell(1,2), cell(1,3)}, 2), P({cell(1,4), cell(2,4)}, 20), P({cell(1,5), cell(1,6), cell(2,6), cell(3,6)}, 6), P({cell(2,2), cell(2,3)}, 3), P({cell(2,5), cell(3,5)}, 3), P({cell(3,1), cell(3,2), cell(4,1), cell(4,2)}, 240), P({cell(3,3), cell(3,4)}, 6), P({cell(4,3), cell(5,3)}, 6), P({cell(4,4), cell(5,4), cell(5,5)}, 7), P({cell(4,5), cell(4,6)}, 30), P({cell(5,1), cell(5,2)}, 6), P({cell(5,6), cell(6,6)}, 9), P({cell(6,1), cell(6,2), cell(6,3)}, 8), P({cell(6,4), cell(6,5)}, 2) ]; Solver m(); var{int} x[1..n, 1..n](m, R); // // assumption: only the segments with 2 cells can be minus or div. // function void calc(Solver m, set{cell} cc, var{int}[,] x, int res) { if (cc.getSize() == 2) { var{int} a(m, x.getRange(0)); var{int} b(m, x.getRange(0)); m.post(a == x[cc.atRank(0).r,cc.atRank(0).c]); m.post(b == x[cc.atRank(1).r,cc.atRank(1).c]); m.post( (a + b == res) || (a * b == res) || (a * res == b) || (b * res == a) || (a - b == res) || (b - a == res) ); } else { m.post( sum(i in cc) x[i.r, i.c] == res || prod(i in cc) x[i.r, i.c] == res ); } } Integer num_solutions(0); exploreall { // all rows and columns 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])); // the specific problem forall(i in 1..num_p) { calc(m, problem[i].c, x, problem[i].res); } } using { label(m); 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;