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
/*
 
  n-queens problem in Comet.
 
  From Pascal Van Hentenryck "The OPL Optimization Programming Language",
  page 34.
 
 
  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();
 
cout << "Number of Variables: ";
int n = cin.getInt();
range Domain = 1..n;
 
Solver<CP> m();
var<CP>{int} queens[Domain](m, Domain);
 
 
Integer num_solutions(0);
 
// exploreall<m> {
explore<m> {
 
  forall(i in 1..n, j in 1..n : i < j) {
    m.post(queens[i] != queens[j]);
    m.post(queens[i] + i != queens[j] + j);
    m.post(queens[i] - i != queens[j] - j);
  }
     
 
} using {
       
  labelFF(m);
  num_solutions++;
  cout << queens << 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;