this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2001
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <gecode/driver.hh>
35#include <gecode/int.hh>
36#include <gecode/minimodel.hh>
37
38using namespace Gecode;
39
40/**
41 * \brief %Example: DONALD+GERALD=ROBERT puzzle
42 *
43 * Well-known cryptoarithmetic puzzle of unknown origin.
44 *
45 * \ingroup Example
46 *
47 */
48class Donald : public Script {
49private:
50 /// Number of letters
51 static const int nl = 10;
52 /// Array of letters
53 IntVarArray le;
54public:
55 /// Model variants
56 enum {
57 MODEL_SINGLE, ///< Use single linear equation
58 MODEL_CARRY ///< Use carries
59 };
60 /// Actual model
61 Donald(const Options& opt)
62 : Script(opt), le(*this,nl,0,9) {
63 IntVar
64 d(le[0]), o(le[1]), n(le[2]), a(le[3]), l(le[4]),
65 g(le[5]), e(le[6]), r(le[7]), b(le[8]), t(le[9]);
66 rel(*this, d, IRT_NQ, 0);
67 rel(*this, g, IRT_NQ, 0);
68 rel(*this, r, IRT_NQ, 0);
69
70 distinct(*this, le, opt.ipl());
71
72 switch (opt.model()) {
73 case MODEL_SINGLE:
74 rel(*this, 100000*d+10000*o+1000*n+100*a+10*l+d
75 + 100000*g+10000*e+1000*r+100*a+10*l+d
76 == 100000*r+10000*o+1000*b+100*e+10*r+t,
77 opt.ipl());
78 break;
79 case MODEL_CARRY:
80 {
81 IntVar c0(*this,0,1), c1(*this,0,1), c2(*this,0,1),
82 c3(*this,0,1), c4(*this,0,1);
83 rel(*this, d+d == t+10*c0, opt.ipl());
84 rel(*this, c0+l+l == r+10*c1, opt.ipl());
85 rel(*this, c1+a+a == e+10*c2, opt.ipl());
86 rel(*this, c2+n+r == b+10*c3, opt.ipl());
87 rel(*this, c3+o+e == o+10*c4, opt.ipl());
88 rel(*this, c4+d+g == r, opt.ipl());
89 }
90 break;
91 default: GECODE_NEVER;
92 }
93
94 branch(*this, le, INT_VAR_SIZE_MIN(), INT_VAL_MAX());
95 }
96 /// Constructor for cloning \a s
97 Donald(Donald& s) : Script(s) {
98 le.update(*this, s.le);
99 }
100 /// Copy during cloning
101 virtual Space*
102 copy(void) {
103 return new Donald(*this);
104 }
105 /// Print solution
106 virtual void
107 print(std::ostream& os) const {
108 os << "\t" << le << std::endl;;
109 }
110
111};
112
113
114/** \brief Main-function
115 * \relates Donald
116 */
117int
118main(int argc, char* argv[]) {
119 Options opt("Donald+Gerald=Robert");
120 opt.model(Donald::MODEL_SINGLE);
121 opt.model(Donald::MODEL_SINGLE, "single", "use single linear equation");
122 opt.model(Donald::MODEL_CARRY, "carry", "use carry");
123 opt.solutions(0);
124 opt.iterations(1500);
125 opt.parse(argc,argv);
126 Script::run<Donald,DFS,Options>(opt);
127 return 0;
128}
129
130// STATISTICS: example-any
131