Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. //
  2. //  Gecode-SudokuSolver - Solves any 9x9 sudoku-board.
  3. //
  4. #include <iostream>
  5. #include <gecode/int.hh>
  6. #include <gecode/minimodel.hh>
  7. #include <gecode/search.hh>
  8. #include <fstream>
  9. #include <iomanip>      // std::setw
  10. #include <random>
  11. std::random_device rd;
  12. std::default_random_engine gen(rd());
  13. std::uniform_int_distribution<int> dist(1, 81);
  14.  
  15. using namespace Gecode;
  16. int board[81];
  17.  
  18. void readFromFile() {       // Load sudoku from file into board.
  19.  
  20.     std::ifstream innfil;
  21.     innfil.open("SolvedSudoku.txt");
  22.  
  23.     if (innfil.is_open()) {
  24.         while (!innfil.eof()) {
  25.             for (int i = 0; i < 81; i++) {
  26.                 innfil >> board[i];
  27.             }
  28.         }
  29.     }
  30.     innfil.close();
  31. }
  32.  
  33.  
  34. class Sudoku : public Space {
  35. protected:
  36.     IntVarArray l;
  37.  
  38. public:
  39.     Sudoku(void) : l(*this, 81, 1, 9) {
  40.  
  41.        
  42.     }
  43.  
  44.     void fill() {
  45.         // Read sudoku from board
  46.         for (int i = 0; i < 81; i++) {
  47.             if (board[i] != 0) {                        // Copy if value is not zero
  48.                 rel(*this, l[i], IRT_EQ, board[i]);
  49.             }
  50.         }
  51.         this->printBoard();
  52.     }
  53.  
  54.     void deleteOne() {
  55.         int r = dist(gen);
  56.         std::cout << r << std::endl;
  57.         IntVar i(*this, 1, 9);
  58.         rel(*this, l[r], IRT_EQ, i);
  59.  
  60.         this->printBoard();
  61.     }
  62.  
  63.     void checkUnique() {
  64.         // Checks for "uniqueness" in the 9x9 grid, for each value in the row and column
  65.         for (int i = 0; i < 9; i++) {
  66.             for (int j = i + 1; j < 9; j++) {
  67.                 for (int k = 0; k < 9; k++) {
  68.                     rel(*this, l[i + 9 * k] != l[j + 9 * k]); // check rows
  69.                     rel(*this, l[9 * i + k] != l[9 * j + k]); // check columns
  70.                 }
  71.             }
  72.         }
  73.  
  74.         // Checks the smaller 3*3 grids within the sudoku board
  75.         for (int x = 0; x < 3; x++) {
  76.             for (int y = x + 1; y < 3; y++) {
  77.                 for (int z = 0; z < 3; z++) {  // Check only current part of board
  78.                     rel(*this, l[x + 3 * z] != l[y + 3 * z]);   // check rows
  79.                     rel(*this, l[3 * x + z] != l[3 * y + z]);   // check collumns
  80.                 }
  81.             }
  82.         }
  83.  
  84.         branch(*this, l, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
  85.     }
  86.  
  87.     Sudoku(Sudoku& s) : Space(s) {
  88.         l.update(*this, s.l);
  89.     }
  90.  
  91.     virtual Space* copy() {
  92.         return new Sudoku(*this);
  93.     }
  94.  
  95.     void print(void) const {
  96.         for (int i = 0; i < 81; i++) {
  97.             if (!(i % 9)) {
  98.                 std::cout << std::endl;
  99.             }
  100.             std::cout << std::setw(13) << l[i] << " ";
  101.  
  102.         }
  103.     }
  104.  
  105.     void printBoard(void) const {
  106.         for (int i = 0; i < 81; i++) {
  107.             if (!(i % 9)) {
  108.                 std::cout << std::endl;
  109.             }
  110.             std::cout << l[i] << " ";
  111.         }
  112.         std::cout << std::endl;
  113.     }
  114.  
  115. };
  116.  
  117.  
  118.  
  119.  
  120. int main(int argc, char* argv[])
  121. {
  122.     readFromFile();
  123.  
  124.     try {
  125.         Sudoku* m = new Sudoku;
  126.         std::cout << "The contents of the sudokuboard before search begins:" << std::endl;
  127.         m->printBoard();
  128.  
  129.         // DFS = depth first search
  130.         DFS<Sudoku> e(m);
  131.         delete m;
  132.  
  133.         // We loop through all solutions to the constraint problem
  134.         int count = 0;
  135.         while (Sudoku* s = e.next()) {
  136.             s->fill();
  137.             s->deleteOne();
  138.  
  139.             s->checkUnique();
  140.             //std::cout << std::endl << std::endl << "The contents of the sodukoboard after search is finished:" << std::endl;
  141.             //s->printBoard();
  142.             count++;
  143.             delete s;
  144.         }
  145.         if (count == 0) {
  146.             std::cout << std::endl << "The puzzle has no solutions" << std::endl;
  147.         }
  148.         else if (count == 1) {
  149.             std::cout << std::endl << "The puzzle has one solution" << std::endl;
  150.         }
  151.         else if (count > 1) {
  152.             std::cout << std::endl << "The puzzle has " << count << " solutions" << std::endl;
  153.         }
  154.     }
  155.     catch (Exception e) {
  156.         std::cerr << "Gecode Exception: " << e.what() << std::endl;
  157.         return 1;
  158.     }
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement