44 using namespace Gecode;
65 class Bishops :
public Space {
69 bool valid_pos(
int i,
int j) {
70 return (i >= 0 && i < n) && (j >= 0 && j < n);
73 : n(size), k(*
this,n*n,0,1) {
75 for (
int l = n; l--; ) {
76 const int il = (n-1) - l;
78 for (
int i = 0; i <= l; ++
i) {
81 d3[
i] = kb((n-1)-i-il, i);
82 d4[
i] = kb((n-1)-i, i+il);
98 Bishops(
bool share, Bishops& s) :
Space(share,s), n(s.n) {
99 k.update(*
this, share, s.k);
101 virtual Space* copy(
bool share) {
102 return new Bishops(share,*
this);
109 Bishops* prob =
new Bishops(size);
113 while (Bishops* s = e.
next()) {
114 for (
int i = size*size;
i--; )
115 ia[
i] = s->k[
i].val();
209 return (i >= 0 && i < n) &&
215 static const int kmoves[4][2] = {
216 {-1,2}, {1,2}, {2,-1}, {2,1}
219 for (
int x = n; x--; )
220 for (
int y = n; y--; )
221 for (
int i = 4;
i--; )
222 if (valid_pos(x+kmoves[
i][0], y+kmoves[i][1]))
226 kb(x+kmoves[i][0], y+kmoves[i][1]),
240 s(*this, n*n, 0, PMAX-1),
241 queens(*this, n, 0, n-1),
242 rooks(*this, n, 0, n-1),
243 knights(*this, n*n, 0, 1) {
244 const int nkval =
sizeof(
kval)/
sizeof(
int);
245 const int nn = n*n, q = n,
r = n,
b = (2*n)-2,
246 k = n <= nkval ?
kval[n-1] :
kval[nkval-1];
247 const int e = nn - (q + r + b + k);
249 assert(nn == (e + q + r + b + k));
264 for (
int i = 0;
i < n; ++
i) {
286 for (
int l = n; l--; ) {
287 const int il = (n-1) - l;
289 for (
int i = 0;
i <= l; ++
i) {
293 d4[
i] =
m((n-1)-
i,
i+il);
309 for (
int i = s.
size();
i--; )
316 for(
int i = n*n;
i--; )
317 knights[
i] =
expr(*
this, (s[
i] == K));
318 knight_constraints();
327 for (
int i = n;
i--; )
345 :
Script(share,e), n(e.n) {
346 s.update(*
this, share, e.
s);
347 queens.update(*
this, share, e.
queens);
348 rooks.update(*
this, share, e.
rooks);
349 knights.update(*
this, share, e.
knights);
363 names[E] =
'.'; names[Q] =
'Q'; names[R] =
'R';
364 names[B] =
'B'; names[K] =
'K';
365 const char* sep = n < 8 ?
"\t\t" :
"\t";
367 for (
int r = 0;
r < n; ++
r){
370 for (
int c = 0;
c < n; ++
c) {
372 os << names[
m(
r,
c).val()];
378 for (
int p = 0; p < PMAX; ++p) {
379 if (p == E)
continue;
381 for (
int c = 0;
c < n; ++
c) {
383 if (
m(
r,
c).val() == p)
407 "Use extensional propagation for bishops-placement");
410 "Use decomposed propagation for bishops-placement");
413 opt.
parse(argc,argv);
414 if (opt.
size() < 5) {
415 std::cerr <<
"Error: size must be at least 5" << std::endl;
418 init_bishops(opt.
size());
419 Script::run<CrowdedChess,DFS,SizeOptions>(
opt);