File: word-square.cpp

package info (click to toggle)
gecode 3.4.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,136 kB
  • ctags: 16,905
  • sloc: cpp: 124,581; perl: 2,004; makefile: 1,453; sh: 247
file content (168 lines) | stat: -rw-r--r-- 5,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Hkan Kjellerstrand <hakank@bonetmail.com>
 *     Mikael Lagerkvist <lagerkvist@gecode.org>
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Hkan Kjellerstrand, 2009
 *     Mikael Lagerkvist, 2009
 *     Christian Schulte, 2009
 *
 *  Last modified:
 *     $Date: 2010-07-14 12:40:03 +0200 (Wed, 14 Jul 2010) $ by $Author: schulte $
 *     $Revision: 11185 $
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <gecode/driver.hh>

#include <gecode/int.hh>
#include <gecode/minimodel.hh>

#include "examples/scowl.hpp"

using namespace Gecode;

/**
 * \brief %Example: Word-square puzzle
 *
 *  From http://en.wikipedia.org/wiki/Word_square:
 *  A word square is a special case of acrostic. It consists of a set of words,
 *  all having the same number of letters as the total number of words (the
 *  "order" of the square); when the words are written out in a square grid
 *  horizontally, the same set of words can be read vertically.
 *
 * \ingroup ExProblem
 *
 */
class WordSquare : public Script {
protected:
  /// Length of words
  const int w_l;
  /// The array of letters
  IntVarArray letters;
public:
  /// Branching variants
  enum {
    BRANCH_WORDS,   ///< Branch on word variables
    BRANCH_LETTERS, ///< Branch on letter variables
  };
  /// Actual model
  WordSquare(const SizeOptions& opt)
    : w_l(opt.size()), letters(*this, w_l*w_l) {

    // Initialize letters
    Matrix<IntVarArray> ml(letters, w_l, w_l);
    for (int i=0; i<w_l; i++)
      for (int j=i; j<w_l; j++)
        ml(i,j) = ml(j,i) = IntVar(*this, 'a','z');
    
    // Number of words with that length
    const int n_w = dict.words(w_l);

    // Initialize word array
    IntVarArgs words(*this, w_l, 0, n_w-1);

    // All words must be different
    distinct(*this, words);

    // Link words with letters
    for (int i=0; i<w_l; i++) {
      // Map each word to i-th letter in that word
      IntSharedArray w2l(n_w);
      for (int n=n_w; n--; )
        w2l[n]=dict.word(w_l,n)[i];
      for (int j=0; j<w_l; j++)
        element(*this, w2l, words[j], ml(i,j));
    }

    // Symmetry breaking: the last word must be later in the wordlist
    rel(*this, words[0], IRT_LE, words[w_l-1]);

    switch (opt.branching()) {
    case BRANCH_WORDS:
      // Branch by assigning words
      branch(*this, words, INT_VAR_SIZE_MIN, INT_VAL_SPLIT_MIN);
      break;
    case BRANCH_LETTERS:
      // Branch by assigning letters
      branch(*this, letters, INT_VAR_SIZE_AFC_MIN, INT_VAL_MIN);
      break;
    }
  }
  /// Constructor for cloning \a s
  WordSquare(bool share, WordSquare& s) 
    : Script(share,s), w_l(s.w_l) {
    letters.update(*this, share, s.letters);
  }
  /// Copy during cloning
  virtual Space*
  copy(bool share) {
    return new WordSquare(share,*this);
  }
  /// Print solution
  virtual void
  print(std::ostream& os) const {
    Matrix<IntVarArray> ml(letters, w_l, w_l);
    for (int i=0; i<w_l; i++) {
      os << "\t\t";
      for (int j=0; j<w_l; j++)
        if (ml(i,j).assigned()) {
          os << static_cast<char>(ml(i,j).val());
        } else {
          os << ".";
        }
      os << std::endl;
    }
    os << std::endl;
  }
};


/** \brief Main-function
 *  \relates WordSquare
 */
int
main(int argc, char* argv[]) {
  FileSizeOptions opt("WordSquare");
  opt.size(6);
  opt.branching(WordSquare::BRANCH_LETTERS);
  opt.branching(WordSquare::BRANCH_WORDS,   "words");
  opt.branching(WordSquare::BRANCH_LETTERS, "letters");
  opt.parse(argc,argv);
  dict.init(opt.file());
  if (opt.size() > static_cast<unsigned int>(dict.len())) {
    std::cerr << "Error: size must be between 0 and "
              << dict.len() << std::endl;
    return 1;
  }
  Script::run<WordSquare,DFS,SizeOptions>(opt);
  return 0;
}

// STATISTICS: example-any