sudoku.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Mikael Lagerkvist <lagerkvist@gecode.org>
* Guido Tack <tack@gecode.org>
* Christian Schulte <schulte@gecode.org>
*
* 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 <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
#include <string>
#include <cmath>
#include <cctype>
using namespace Gecode;
#include "examples/sudoku-instances.hh"
class Sudoku : public Script {
protected:
const int n;
IntVarArray x;
public:
Sudoku(const SizeOptions& opt)
: Script(opt),
n(example_size(examples[opt.size()])),
x(*this, n*n*n*n, 1, n*n) {
const int nn = n*n;
Matrix<IntVarArray> m(x, nn, nn);
// Constraints for rows and columns
for (int i=0; i<nn; i++) {
distinct(*this, m.row(i), opt.ipl());
distinct(*this, m.col(i), opt.ipl());
}
// Constraints for squares
for (int i=0; i<nn; i+=n) {
for (int j=0; j<nn; j+=n) {
distinct(*this, m.slice(i, i+n, j, j+n), opt.ipl());
}
}
// Fill-in predefined fields
for (int i=0; i<nn; i++)
for (int j=0; j<nn; j++)
if (int v = sudokuField(examples[opt.size()], nn, i, j))
rel(*this, m(i,j), IRT_EQ, v );
branch(*this, x, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
}
Sudoku(Sudoku& s) : Script(s), n(s.n) {
x.update(*this, s.x);
}
virtual Space*
copy(void) {
return new Sudoku(*this);
}
virtual void
print(std::ostream& os) const {
os << " ";
for (int i = 0; i<n*n*n*n; i++) {
if (x[i].assigned()) {
if (x[i].val()<10)
os << x[i] << " ";
else
os << (char)(x[i].val()+'A'-10) << " ";
}
else
os << ". ";
if((i+1)%(n*n) == 0)
os << std::endl << " ";
}
os << std::endl;
}
};
int
main(int argc, char* argv[]) {
SizeOptions opt("Sudoku");
opt.size(0);
opt.ipl(IPL_DOM);
opt.solutions(1);
opt.parse(argc,argv);
if (opt.size() >= n_examples) {
std::cerr << "Error: size must be between 0 and "
<< n_examples-1 << std::endl;
return 1;
}
Script::run<Sudoku,DFS,SizeOptions>(opt);
return 0;
}
// STATISTICS: example-any
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::Matrix
Matrix-interface for arrays.
Definition minimodel.hh:2161
Gecode::Matrix::slice
Slice< A > slice(int fc, int tc, int fr, int tr) const
Access slice of the matrix.
Definition matrix.hpp:171
Gecode::Matrix::col
Slice< A > col(int c) const
Access column c.
Definition matrix.hpp:183
Gecode::Matrix::row
Slice< A > row(int r) const
Access row r.
Definition matrix.hpp:177
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition driver.hh:675
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Sudoku
Base class for Sudoku puzzles.
Definition sudoku-advanced.cpp:55
Sudoku::example_size
int example_size(const char *s)
The size of an instance.
Definition sudoku-instances.hh:1852
Sudoku::Sudoku
Sudoku(const SizeOptions &opt)
Constructor.
Definition sudoku.cpp:64
Sudoku::copy
virtual Space * copy(void)
Perform copying during cloning.
Definition sudoku.cpp:101
Sudoku::examples
const char * examples[]
The specifications.
Definition sudoku-instances.hh:54
Sudoku::x
IntVarArray x
Values for the fields.
Definition sudoku.cpp:60
Sudoku::sudokuField
int sudokuField(const char *s, int n, int i, int j)
Return value at position (i,j) in the example s of size n.
Definition sudoku-instances.hh:1860
Sudoku::n
const int n
The size of the problem.
Definition sudoku-advanced.cpp:58
Sudoku::print
virtual void print(std::ostream &os) const
Print solution.
Definition sudoku.cpp:107
Sudoku::Sudoku
Sudoku(Sudoku &s)
Constructor for cloning s.
Definition sudoku.cpp:95
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.hh
Gecode::Script
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
Gecode::branch
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::INT_VAR_AFC_SIZE_MAX
IntVarBranch INT_VAR_AFC_SIZE_MAX(double d=1.0, BranchTbl tbl=nullptr)
Select variable with largest accumulated failure count divided by domain size with decay factor d.
Definition var.hpp:236
Gecode::distinct
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
Gecode::INT_VAL_MIN
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55
Test::opt
Options opt
The options.
Definition test.cpp:97
sudoku-instances.hh
main
int main(int argc, char *argv[])
Definition test.cpp:208