/* Survo puzzle in Comet. http://en.wikipedia.org/wiki/Survo_Puzzle """ Survo puzzle is a kind of logic puzzle presented (in April 2006) and studied by Seppo Mustonen. The name of the puzzle is associated to Mustonen's Survo system which is a general environment for statistical computing and related areas. In a Survo puzzle the task is to fill an m * n table by integers 1,2,...,m*n so that each of these numbers appears only once and their row and column sums are equal to integers given on the bottom and the right side of the table. Often some of the integers are given readily in the table in order to guarantee uniqueness of the solution and/or for making the task easier. """ See also http://www.survo.fi/english/index.html http://www.survo.fi/puzzles/index.html References: Mustonen, S. (2006b). "On certain cross sum puzzles" http://www.survo.fi/papers/puzzles.pdf Mustonen, S. (2007b). "Enumeration of uniquely solvable open Survo puzzles." http://www.survo.fi/papers/enum_survo_puzzles.pdf Kimmo Vehkalahti: "Some comments on magic squares and Survo puzzles" http://www.helsinki.fi/~kvehkala/Kimmo_Vehkalahti_Windsor.pdf R code: http://koti.mbnet.fi/tuimala/tiedostot/survo.R 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 r = 3; // number of rows int c = 4 ; // number of columns int rowsums[1..r] = [30,18,30]; int colsums[1..c] = [27,16,10,25]; // known values (the clues) int matrix[1..r, 1..c] = [ [0, 6, 0, 0], [8, 0, 0, 0], [0, 0, 3, 0] ]; Solver m(); // the solution: var{int} x[1..r, 1..c](m, 1..r*c); Integer num_solutions(0); exploreall { m.post(alldifferent(all(i in 1..r, j in 1..c) x[i,j] )); forall(i in 1..r, j in 1..c : matrix[i,j] > 0) m.post(matrix[i,j] == x[i,j]); forall(i in 1..r) m.post(sum(j in 1..c) (x[i,j]) == rowsums[i]); forall(j in 1..c) m.post(sum(i in 1..r) (x[i,j]) == colsums[j]); } using { forall(i in 1..r, j in 1..c : !x[i,j].bound()) {// by (-x[i,j].getSize()) { label(x[i,j]); } forall(i in 1..r) { forall(j in 1..c) { cout << x[i,j] << " "; } cout << endl; } cout << endl; num_solutions := num_solutions + 1; } 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;