1 #include <cstdio>
2 #include <cassert>
3 #include <chuffed/core/engine.h>
4 #include <chuffed/core/propagator.h>
5 #include <chuffed/branching/branching.h>
6 #include <chuffed/vars/modelling.h>
7 #include <chuffed/ldsb/ldsb.h>
8
9 class Queens : public Problem {
10 public:
11 int const n;
12 vec<IntVar*> x; // placement of queens
13
Queens(int _n)14 Queens(int _n) : n(_n) {
15
16 createVars(x, n, 1, n);
17
18 vec<int> a, b;
19 for (int i = 0; i < n; i++) {
20 a.push(i);
21 b.push(-i);
22 }
23
24 all_different(x);
25 all_different_offset(a, x);
26 all_different_offset(b, x);
27
28 branch(x, VAR_INORDER, VAL_MIN);
29 // branch(x, VAR_SIZE_MIN, VAL_MIN);
30
31 output_vars(x);
32
33 if (so.ldsb) {
34 // horizontal flip
35 vec<IntVar*> sym1;
36
37 for (int i = 0; i < n/2; i++) {
38 sym1.push(x[i]);
39 }
40 for (int i = 0; i < n/2; i++) {
41 sym1.push(x[n-i-1]);
42 }
43
44 var_seq_sym_ldsb(2, n/2, sym1);
45
46 // vertical flip sym
47
48 vec<int> sym2;
49 for (int i = 0; i < n; i++) sym2.push(i+1);
50 for (int i = 0; i < n; i++) sym2.push(n-i);
51
52 val_seq_sym_ldsb(2, n, x, sym2);
53
54 } else if (so.sym_static) {
55
56 int_rel(x[0], IRT_LT, x[n-1]);
57 int_rel(x[0], IRT_LE, (n+1)/2);
58 IntVar* t = newIntVar(1,n+1);
59 int_plus(x[0], x[n-1], t);
60
61 }
62 }
63
print(std::ostream & os)64 void print(std::ostream& os) {
65 for (int i = 0; i < n; i++) {
66 for (int j = 0; j < n; j++) {
67 os << ((x[i]->getVal()-1 == j) ? 1 : 0) << ", ";
68 }
69 os << "\n";
70 }
71 os << "\n";
72 }
73
74 };
75
main(int argc,char ** argv)76 int main(int argc, char** argv) {
77 parseOptions(argc, argv);
78
79 int n;
80
81 assert(argc == 2);
82 n = atoi(argv[1]);
83
84 engine.solve(new Queens(n));
85
86 return 0;
87 }
88
89
90
91