/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Mikael Lagerkvist * Guido Tack * Christian Schulte * * Copyright: * Mikael Lagerkvist, 2005 * Guido Tack, 2005 * Christian Schulte, 2005 * * 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 #include #include #ifdef GECODE_HAS_SET_VARS #include #endif #include #include #include using namespace Gecode; #include /// Base class for %Sudoku puzzles class Sudoku : public Script { protected: /// The size of the problem const int n; public: #ifdef GECODE_HAS_SET_VARS /// Model variants enum { MODEL_INT, ///< Use integer constraints MODEL_SET, ///< Use set constraints MODEL_MIXED ///< Use both integer and set constraints }; #endif // Branching variants enum { BRANCH_NONE, ///< Use lexicographic ordering BRANCH_SIZE, ///< Use minimum size BRANCH_SIZE_DEGREE, ///< Use minimum size over degree BRANCH_SIZE_AFC, ///< Use minimum size over afc BRANCH_AFC ///< Use maximum afc }; /// Constructor Sudoku(const SizeOptions& opt) : Script(opt), n(example_size(examples[opt.size()])) {} /// Constructor for cloning \a s Sudoku(Sudoku& s) : Script(s), n(s.n) {} }; /** * \brief %Example: Solving %Sudoku puzzles using integer constraints * * \ingroup Example */ class SudokuInt : virtual public Sudoku { protected: /// Values for the fields IntVarArray x; public: #ifdef GECODE_HAS_SET_VARS /// Propagation variants enum { PROP_NONE, ///< No additional constraints PROP_SAME, ///< Use "same" constraint with integer model }; #endif /// Constructor SudokuInt(const SizeOptions& opt) : Sudoku(opt), x(*this, n*n*n*n, 1, n*n) { const int nn = n*n; Matrix m(x, nn, nn); // Constraints for rows and columns for (int i=0; i m, int bc, int i, int j) { return m.slice(bc*n+i, bc*n+i+1, j*n, (j+1)*n); } /// Extract row \a br from block starting at (\a i,\a j) IntVarArgs block_row(Matrix m, int br, int i, int j) { return m.slice(j*n, (j+1)*n, br*n+i, br*n+i+1); } #endif }; #ifdef GECODE_HAS_SET_VARS /** * \brief %Example: Solving %Sudoku puzzles using set constraints * * \ingroup Example */ class SudokuSet : virtual public Sudoku { protected: /// The fields occupied by a certain number SetVarArray y; public: /// Constructor SudokuSet(const SizeOptions& opt) : Sudoku(opt), y(*this,n*n,IntSet::empty,1,n*n*n*n, static_cast(n*n),static_cast(n*n)) { const int nn = n*n; Region r; IntSet* row = r.alloc(nn); IntSet* col = r.alloc(nn); IntSet* block = r.alloc(nn); // Set up the row and column set constants int* dsc = r.alloc(nn); for (int i=0; i(nn); for (int i=0; i= n_examples) { std::cerr << "Error: size must be between 0 and " << n_examples-1 << std::endl; return 1; } #ifdef GECODE_HAS_SET_VARS switch (opt.model()) { case Sudoku::MODEL_INT: Script::run(opt); break; case Sudoku::MODEL_SET: Script::run(opt); break; case Sudoku::MODEL_MIXED: Script::run(opt); break; } #else Script::run(opt); #endif return 0; } // STATISTICS: example-any