Generated on Fri Oct 6 16:26:42 2006 for Gecode/J by doxygen 1.4.7

Queens.java

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2006
00008  *     Guido Tack, 2006
00009  *
00010  *  Last modified:
00011  *     $Date: 2006-09-26 16:26:14 +0200 (Tue, 26 Sep 2006) $ by $Author: zayenz $
00012  *     $Revision: 3704 $
00013  *
00014  *  This file is part of Gecode, the generic constraint
00015  *  development environment:
00016  *     http://www.gecode.org
00017  *
00018  *  See the file "LICENSE" for information on usage and
00019  *  redistribution of this file, and for a
00020  *     DISCLAIMER OF ALL WARRANTIES.
00021  *
00022  */
00023 
00024 package examples;
00025 
00026 import static org.gecode.Gecode.*;
00027 import static org.gecode.GecodeEnumConstants.*;
00028 
00029 import org.gecode.*;
00030 import org.gecode.explorer.Explorer;
00031 
00036 public class Queens extends Space {
00037     private int n;
00038     private VarArray<IntVar> q;
00039 
00040     public Queens(Options opt) {
00041         super();
00042         n = opt.size;
00043         q = new VarArray<IntVar>(this, n, IntVar.class, 0, n-1);
00044         
00045         int c[] = new int[n];
00046 
00047         for (int i=0; i<n; i++)
00048             c[i] = i;
00049         distinct(this, c, q, opt.icl);
00050 
00051         for (int i=0; i<n; i++)
00052             c[i] = -i;
00053         distinct(this, c, q, opt.icl);
00054 
00055         distinct(this, q, opt.icl);
00056 
00057         branch(this, q, BVAR_SIZE_MIN, BVAL_MIN);
00058     }
00059 
00060     public Queens(Boolean share, Queens queens) {
00061         super(share, queens);
00062         n = queens.n;
00063         q = new VarArray<IntVar>(this, share, queens.q);
00064     }
00065 
00066     public String toString() {
00067         String res = "";
00068         
00069         for (int i = 0; i < n; ++i) {
00070             char[] l = new char[n];
00071             for (int j=0;j<n;++j) l[j] = '\u00B7';
00072 
00073             if (q.get(i).assigned()) {
00074                 l[q.get(i).val()] = 'Q';
00075             } else {
00076                 for (Range r : new IntVarRanges(q.get(i)))
00077                   for (int j : r)
00078                     l[j] = 'q';
00079             }
00080             res += new String(l);
00081             res += "\n";
00082         }
00083         return res;
00084     }
00085 
00086     public static void main(String[] args) {
00087         Options opt = new Options();
00088         opt.size = 6;
00089         opt.gui = true;
00090         opt.parse(args);
00091         opt.name = "" + opt.size + "-Queens";
00092 
00093         Queens queens = new Queens(opt);
00094         opt.doSearch(queens);
00095     }
00096 }