Queens.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 }