00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "examples/support.hh"
00023 #include "gecode/minimodel.hh"
00024
00033 class MagicSquare : public Example {
00034 private:
00036 const int n;
00038 IntVarArray x;
00039
00040 public:
00042 MagicSquare(const Options& opt)
00043 : n(opt.size), x(this,n*n,1,n*n) {
00044
00045 const int nn = n*n;
00046
00047
00048 const int s = nn*(nn+1) / (2*n);
00049
00050
00051 MiniModel::Matrix<IntVarArray> m(x, n, n);
00052
00053 for (int i = n; i--; ) {
00054 linear(this, m.row(i), IRT_EQ, s, opt.icl);
00055 linear(this, m.col(i), IRT_EQ, s, opt.icl);
00056 }
00057
00058 {
00059 IntVarArgs d1y(n);
00060 IntVarArgs d2y(n);
00061 for (int i = n; i--; ) {
00062 d1y[i] = m(i,i);
00063 d2y[i] = m(n-i-1,i);
00064 }
00065 linear(this, d1y, IRT_EQ, s, opt.icl);
00066 linear(this, d2y, IRT_EQ, s, opt.icl);
00067 }
00068
00069
00070 distinct(this, x, opt.icl);
00071
00072
00073 rel(this, m(0,0), IRT_GR, m(0,n-1));
00074 rel(this, m(0,0), IRT_GR, m(n-1,0));
00075
00076 branch(this, x, BVAR_SIZE_MIN, BVAL_SPLIT_MIN);
00077 }
00078
00080 MagicSquare(bool share, MagicSquare& s) : Example(share,s), n(s.n) {
00081 x.update(this, share, s.x);
00082 }
00083
00085 virtual Space*
00086 copy(bool share) {
00087 return new MagicSquare(share,*this);
00088 }
00090 virtual void
00091 print(void) {
00092
00093 MiniModel::Matrix<IntVarArray> m(x, n, n);
00094 for (int i = 0; i<n; i++) {
00095 std::cout << "\t";
00096 for (int j = 0; j<n; j++) {
00097 std::cout.width(2);
00098 std::cout << m(i,j) << " ";
00099 }
00100 std::cout << std::endl;
00101 }
00102 }
00103
00104 };
00105
00109 int
00110 main(int argc, char** argv) {
00111 Options opt("MagicSquare");
00112 opt.iterations = 1;
00113 opt.size = 7;
00114 opt.parse(argc,argv);
00115 Example::run<MagicSquare,DFS>(opt);
00116 return 0;
00117 }
00118
00119
00120