00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <gecode/driver.hh>
00035 #include <gecode/int.hh>
00036 #include <gecode/minimodel.hh>
00037
00038 using namespace Gecode;
00039
00048 class Donald : public Script {
00049 private:
00051 static const int nl = 10;
00053 IntVarArray le;
00054 public:
00056 enum {
00057 MODEL_SINGLE,
00058 MODEL_CARRY
00059 };
00061 Donald(const Options& opt)
00062 : Script(opt), le(*this,nl,0,9) {
00063 IntVar
00064 d(le[0]), o(le[1]), n(le[2]), a(le[3]), l(le[4]),
00065 g(le[5]), e(le[6]), r(le[7]), b(le[8]), t(le[9]);
00066 rel(*this, d, IRT_NQ, 0);
00067 rel(*this, g, IRT_NQ, 0);
00068 rel(*this, r, IRT_NQ, 0);
00069
00070 distinct(*this, le, opt.ipl());
00071
00072 switch (opt.model()) {
00073 case MODEL_SINGLE:
00074 rel(*this, 100000*d+10000*o+1000*n+100*a+10*l+d
00075 + 100000*g+10000*e+1000*r+100*a+10*l+d
00076 == 100000*r+10000*o+1000*b+100*e+10*r+t,
00077 opt.ipl());
00078 break;
00079 case MODEL_CARRY:
00080 {
00081 IntVar c0(*this,0,1), c1(*this,0,1), c2(*this,0,1),
00082 c3(*this,0,1), c4(*this,0,1);
00083 rel(*this, d+d == t+10*c0, opt.ipl());
00084 rel(*this, c0+l+l == r+10*c1, opt.ipl());
00085 rel(*this, c1+a+a == e+10*c2, opt.ipl());
00086 rel(*this, c2+n+r == b+10*c3, opt.ipl());
00087 rel(*this, c3+o+e == o+10*c4, opt.ipl());
00088 rel(*this, c4+d+g == r, opt.ipl());
00089 }
00090 break;
00091 default: GECODE_NEVER;
00092 }
00093
00094 branch(*this, le, INT_VAR_SIZE_MIN(), INT_VAL_MAX());
00095 }
00097 Donald(Donald& s) : Script(s) {
00098 le.update(*this, s.le);
00099 }
00101 virtual Space*
00102 copy(void) {
00103 return new Donald(*this);
00104 }
00106 virtual void
00107 print(std::ostream& os) const {
00108 os << "\t" << le << std::endl;;
00109 }
00110
00111 };
00112
00113
00117 int
00118 main(int argc, char* argv[]) {
00119 Options opt("Donald+Gerald=Robert");
00120 opt.model(Donald::MODEL_SINGLE);
00121 opt.model(Donald::MODEL_SINGLE, "single", "use single linear equation");
00122 opt.model(Donald::MODEL_CARRY, "carry", "use carry");
00123 opt.solutions(0);
00124 opt.iterations(1500);
00125 opt.parse(argc,argv);
00126 Script::run<Donald,DFS,Options>(opt);
00127 return 0;
00128 }
00129
00130
00131