Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //Gecode-demo - list with distinct elements (i.e. no two elements are the same)
- //
- #include <iostream>
- #include <fstream>
- #include <gecode/int.hh>
- #include <gecode/minimodel.hh>
- #include <gecode/search.hh>
- using namespace Gecode;
- class DistinctList : public Space {
- protected:
- IntVarArray l, puzzle, newPuzzle;
- public:
- DistinctList(void) : l(*this, 81, 1, 9) {
- // Three possible solutions to this problem.
- // 1. using the core gecode language
- std::ifstream innfil("sudoku1.DTA");
- int n[81];
- if (innfil) {
- while (!innfil.eof()) {
- for (int a = 0; a < 81; a++) {
- innfil >> n[a];
- if (n[a] != 0)
- rel(*this, l[a] == n[a]); //assign value to arrayspace l[]
- }
- }
- }
- // 2. mini-model language
- int boks = 0;
- for (int b = 0; b < 9; b++) {
- if (b % 3 == 0 && b) boks += 18;
- int hopp = 0;
- for (int i = 0; i < 9; i++) {
- if (i % 3 == 0 && i) hopp += 6;
- //cout << "Fra: " << i + hopp + b * 3 + boks << "\n";
- int avst = 0;
- for (int j = i + 1; j < 9; j++) {
- if (j % 3 == 0 && j) avst += 6;
- //cout << "\tj: " << j + avst + hopp + b * 3 + boks << endl;
- rel(*this, l[i + hopp + b * 3 + boks] != l[j + avst + hopp + b * 3 + boks]);
- }
- }
- }
- // for rows and columns
- for (int i = 0; i < 9; i++)
- {
- for (int j = i + 1; j < 9; j++)
- {
- // rows
- for (int k = 0; k < 81; k += 9)
- {
- rel(*this, l[i + k] != l[j + k]);
- }
- // columns
- for (int m = 0; m < 9; m++) {
- rel(*this, l[9 * i + m] != l[9 * j + m]);
- }
- }
- }
- // 3. built in function
- // distinct(*this, l);
- branch(*this, l, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
- }
- DistinctList(DistinctList& s) : Space(s) {
- l.update(*this, s.l);
- }
- Space* copy() {
- return new DistinctList(*this);
- }
- /* jalla som faen print til screen */
- void print(void) const {
- //std::cout << l << std::endl;
- int r = 1;
- for (int i = 0; i < 81; i++)
- {
- std::cout << l[i];
- if (r % 3 == 0)
- {
- std::cout << " ";
- }
- if (r % 9 == 0)
- {
- std::cout << '\n';
- }
- if (r == 27 || r == 54)
- {
- std::cout << '\n';
- }
- r++;
- if (r == 82)
- {
- std::cout << '\n\n';
- std::cout << "-------------------------------------------------------------------\n";
- }
- }
- }
- void oppgave2b(int count)
- {
- puzzle = l;
- bool finished = false;
- while (!finished) {
- newPuzzle = puzzle;
- rel(*this, newPuzzle[rand()%81+1] == 0);
- if (count > 1) finished = true;
- else
- puzzle = newPuzzle;
- }
- }
- };
- int nomain(int argc, char* argv[])
- {
- DistinctList* m = new DistinctList;
- std::cout << "The contents of the space before search begins" << std::endl;
- m->print();
- // We initialise and get ready for search - search has not yet begun
- // DFS = depth first search
- DFS<DistinctList> e(m);
- delete m;
- // We loop through all solutions to the constraint problem
- int count = 0;
- while (DistinctList* s = e.next()) {
- s->print(); delete s;
- count++;
- }
- std::cout << count;
- m->oppgave2b(count);
- std::cout << "Number of elements: " << count << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement