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: SEND+MORE=MONEY puzzle
42 *
43 * Well-known cryptoarithmetic puzzle.
44 * Henry Dudeney, Strand Magazine, July 1924.
45 *
46 * \ingroup Example
47 *
48 */
49class Money : public Script {
50protected:
51 /// Number of letters
52 static const int nl = 8;
53 /// Array of letters
54 IntVarArray le;
55public:
56 /// Model variants
57 enum {
58 MODEL_SINGLE, ///< Use single linear equation
59 MODEL_CARRY ///< Use carries
60 };
61 /// Actual model
62 Money(const Options& opt) : Script(opt), le(*this,nl,0,9) {
63 IntVar
64 s(le[0]), e(le[1]), n(le[2]), d(le[3]),
65 m(le[4]), o(le[5]), r(le[6]), y(le[7]);
66
67 if (opt.trace()) {
68 trace(*this, le, opt.trace());
69 trace(*this, opt.trace());
70 }
71
72 rel(*this, s, IRT_NQ, 0);
73 rel(*this, m, IRT_NQ, 0);
74
75 distinct(*this, le, opt.ipl());
76
77 switch (opt.model()) {
78 case MODEL_SINGLE:
79 rel(*this, 1000*s+100*e+10*n+d
80 + 1000*m+100*o+10*r+e
81 == 10000*m+1000*o+100*n+10*e+y,
82 opt.ipl());
83 break;
84 case MODEL_CARRY:
85 {
86 IntVar c0(*this,0,1), c1(*this,0,1), c2(*this,0,1), c3(*this,0,1);
87 rel(*this, d+e == y+10*c0, opt.ipl());
88 rel(*this, c0+n+r == e+10*c1, opt.ipl());
89 rel(*this, c1+e+o == n+10*c2, opt.ipl());
90 rel(*this, c2+s+m == o+10*c3, opt.ipl());
91 rel(*this, c3 == m, opt.ipl());
92 }
93 break;
94 default: GECODE_NEVER;
95 }
96
97 branch(*this, le, INT_VAR_SIZE_MIN(), INT_VAL_MIN(), nullptr,
98 [](const Space&, const Brancher&, unsigned int a,
99 IntVar, int i, const int& n,
100 std::ostream& o) {
101 static char name[8] = {'s','e','n','d',
102 'm','o','r','y'};
103 o << name[i]
104 << ((a == 0) ? " = " : " != ")
105 << n;
106 });
107 }
108 /// Print solution
109 virtual void
110 print(std::ostream& os) const {
111 os << "\t" << le << std::endl;
112 }
113
114 /// Constructor for cloning \a s
115 Money(Money& s) : Script(s) {
116 le.update(*this, s.le);
117 }
118 /// Copy during cloning
119 virtual Space*
120 copy(void) {
121 return new Money(*this);
122 }
123};
124
125/** \brief Main-function
126 * \relates Money
127 */
128int
129main(int argc, char* argv[]) {
130 Options opt("SEND+MORE=MONEY");
131 opt.model(Money::MODEL_SINGLE);
132 opt.model(Money::MODEL_SINGLE, "single", "use single linear equation");
133 opt.model(Money::MODEL_CARRY, "carry", "use carry");
134 opt.solutions(0);
135 opt.iterations(20000);
136 opt.parse(argc,argv);
137 Script::run<Money,DFS,Options>(opt);
138 return 0;
139}
140
141// STATISTICS: example-any
142