93 for (
int i =
n*
n; i--; ) {
95 rel(*
this,
w[i] == (
U ||
A[i]));
97 rel(*
this, !
w[i] || !
b[i]);
108 rel(*
this,
q <= unknowns);
122 U.update(*
this, s.
U);
123 W.update(*
this, s.
W);
124 w.update(*
this, s.
w);
125 b.update(*
this, s.
b);
126 q.update(*
this, s.
q);
141 for (
int i = 0; i <
n*
n; ++i) {
142 if (
w[i].assigned() &&
w[i].val()) os <<
"W";
143 else if (
b[i].assigned() &&
b[i].val()) os <<
"B";
144 else if (!
w[i].assigned() && !
b[i].assigned()) os <<
" ";
146 if ((i+1)%
n == 0) os << std::endl << (i!=(
n*
n-1)?
"\t":
"");
148 os <<
"Number of white queens: " <<
q << std::endl << std::endl;
175 virtual void archive(
Archive& e)
const {
182 QueenBranch(
Home home)
185 QueenBranch(
Space& home, QueenBranch&
b)
192 for (
int i = start; i <
q.n*
q.n; ++i)
193 if (!
q.w[i].assigned()) {
206 for (
int i = start; i <
q.n*
q.n; ++i) {
207 if (
q.w[i].assigned())
continue;
212 if (size > maxsize) {
233 const Choice& c =
static_cast<const Choice&
>(_c);
234 bool val = (a == 0) ? c.val : !c.val;
242 std::ostream& o)
const {
243 const Choice& c =
static_cast<const Choice&
>(_c);
244 bool val = (a == 0) ? c.val : !c.val;
245 o <<
"w[" << c.pos <<
"] = " << val;
249 return new (home) QueenBranch(home, *
this);
253 (void)
new (home) QueenBranch(home);
257 return sizeof(*this);
281 opt.
parse(argc,argv);
287 int *p =
new int[std::max(
n*
n, 25)];
289 for (
int i =
n; i--; ) {
290 for (
int j =
n; j--; ) {
301 p[pn++] =
pos(i, j,
n);
302 for (
int k = 8; k--; ) {
303 for (
int l = 0; l <
n
304 && 0 <= (i+l*dir[k][0]) && (i+l*dir[k][0]) <
n
305 && 0 <= (j+l*dir[k][1]) && (j+l*dir[k][1]) <
n; ++l) {
306 p[pn++] =
pos(i+l*dir[k][0], j+l*dir[k][1],
n);
Brancher(Home home)
Constructor for creation.
Choice for performing commit
virtual void archive(Archive &e) const
Archive into e.
static void run(const Options &opt, Script *s=NULL)
Home class for posting propagators
Range iterator for integer sets.
Boolean view for Boolean variables.
ModEvent eq(Space &home, int n)
Restrict domain values to be equal to n.
Range iterator for computing intersection (binary)
Iterator for the unknown ranges of a set variable.
Options for scripts with additional size parameter
virtual Gecode::Choice * choice(Space &home)
Return choice.
virtual void print(const Space &, const Gecode::Choice &_c, unsigned int a, std::ostream &o) const
Print explanation.
virtual ExecStatus commit(Space &home, const Gecode::Choice &_c, unsigned int a)
Perform commit for choice _c and alternative a.
virtual size_t dispose(Space &)
Delete brancher and return its size.
virtual bool status(const Space &home) const
Check status of brancher, return true if alternatives left.
static void post(QueenArmies &home)
Post brancher.
virtual Choice * choice(const Space &, Archive &e)
Return choice.
virtual Actor * copy(Space &home)
Copy brancher during cloning.
QueenArmies(const SizeOptions &opt)
Constructor.
int main(int argc, char *argv[])
Main-function.
int pos(int i, int j, int n)
Position of a piece in a square board.
virtual void print(std::ostream &os) const
Print solution.
BoolVarArray w
The placement of the white queens.
BoolVarArray b
The placement of the black queens.
QueenArmies(QueenArmies &s)
Constructor for cloning.
virtual IntVar cost(void) const
Return solution cost.
IntVar q
The number of white queens placed.
@ BRANCH_NAIVE
Choose variables left to right.
@ BRANCH_SPECIFIC
Choose variable with problem specific strategy.
SetVar W
Set of squares occupied by white queens.
virtual Space * copy(void)
Return copy during cloning.
SetVar U
Set of un-attacked squares.
IntSet * A
Position of a piece in a square board.
void parse(int argc, char *argv[])
Parse commandline arguments.
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Driver::ScriptBase< Driver::IgnoreStepOption< IntMaximizeSpace > > IntMaximizeScript
Base-class for scripts for finding solution of highest integer cost.
void assign(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatAssign vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Assign all x with variable selection vars and value selection vals.
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
@ IRT_GQ
Greater or equal ( )
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode toplevel namespace
BoolAssign BOOL_ASSIGN_MAX(void)
Select largest value.
Post propagator for SetVar SetOpType SetVar SetRelType r
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
BoolVarBranch BOOL_VAR_NONE(void)
Select first unassigned variable.
BoolValBranch BOOL_VAL_MAX(void)
Select largest value.
SetExpr singleton(const LinIntExpr &)
Singleton expression.
@ ES_OK
Execution is okay.
@ ES_FAILED
Execution has resulted in failure.
LinIntExpr cardinality(const SetExpr &)
Cardinality of set expression.