vladkomarr

sudoku

Nov 7th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. //main.cpp
  2. #include <iostream>
  3. #include <grid.h>
  4.  
  5. int main()
  6. {
  7.     grid Grid(9);
  8.     if(Grid.gridFromFile("/home/felblade/sudoku/sudoku/grid.txt"))
  9.         std::cout << "Successful read\n\n";
  10.     else std::cout << "Error\n";
  11.     Grid.output();
  12.     std::cout << std::endl;
  13.     grid Gridd(Grid);
  14.     Gridd.output();
  15.     return 0;
  16. }
  17.  
  18. //grid.h
  19. #ifndef GRID_H
  20. #define GRID_H
  21. #include <iostream>
  22. #include <math.h>
  23. #include <fstream>
  24.  
  25. class grid {
  26. public:
  27.     int gridSize;                               // the size of full grid
  28.     int sectorSize;                             // the size of sector ( sqrt(gridSize)  )
  29.     int **matrix;                               // the dynamic 2d matrix
  30.     grid(int gridSize);                         // constructor with one parameter
  31.     grid(const grid&);                          // constructor of copy
  32.     bool gridFromFile(char *filepath);          // get the grid from file
  33.     void output();                              // prints the grid into console
  34.  
  35.     ///////////////////////////////////////////////////////////////////////////////////////
  36.     //solving task methods
  37.  
  38.     bool findUnassignedLocation(int &row, int &col);
  39.     bool findSolution();
  40.  
  41.     bool usedInRow(int digit, int row);         // checks if same digit in row
  42.     bool usedInCol(int digit, int col);         // checks if same digit in col
  43.     bool usedInBox(int digit, int row, int col);// checks if same digit in box
  44.     bool isSafe(int digit, int row, int col);   // cheks if it is legal to assign digit to current location
  45.  
  46.  
  47. };
  48.  
  49. #endif // GRID_H
  50.  
  51. //grid.cpp
  52. #include "grid.h"
  53.  
  54. grid::grid(int gridSize) {
  55.     this->gridSize = gridSize;
  56.     this->sectorSize = sqrt(gridSize);
  57.     matrix = new int*[gridSize];
  58.     for(int i = 0; i < gridSize; i++)
  59.         matrix[i] = new int[gridSize];
  60. }
  61.  
  62. grid::grid(const grid &prototype) {
  63.     this->gridSize = prototype.gridSize;
  64.     this->sectorSize = prototype.sectorSize;
  65.     matrix = new int*[gridSize];
  66.     for(int i = 0; i < gridSize;i++)
  67.         matrix[i] = new int[gridSize];
  68.     for(int i = 0; i < gridSize; i++)
  69.         for(int j = 0; j < gridSize; j++)
  70.             matrix[i][j] = prototype.matrix[i][j];
  71. }
  72.  
  73. bool grid::gridFromFile(char *filepath) {
  74.     FILE *fp;
  75.     if(fp = std::fopen(filepath, "r")) {
  76.         for(int i = 0; i < gridSize; i++)
  77.             for(int j = 0; j < gridSize; j++)
  78.                 std::fscanf(fp, "%i", &matrix[i][j]);
  79.         fclose(fp);
  80.         return true;
  81.     }
  82.     else
  83.         return false;
  84. }
  85.  
  86. void grid::output() {
  87.     for(int i = 0; i < gridSize; i++) {
  88.         for(int j = 0; j < gridSize; j++) {
  89.             std::cout << matrix[i][j] << " ";
  90.             if(!((j+1)%sectorSize)) std::cout << " ";
  91.         }
  92.         if(!((i+1)%sectorSize)) std::cout << std::endl;
  93.         std::cout << std::endl;
  94.     }
  95. }
  96.  
  97. bool grid::usedInRow(int digit, int row) {
  98.     for(int col = 0; col < gridSize; col++)
  99.         if(matrix[row][col] == digit)
  100.             return true;
  101.     return false;
  102. }
  103.  
  104. bool grid::usedInCol(int digit, int col) {
  105.     for(int row = 0; row < gridSize; row++)
  106.         if(matrix[row][col] == digit)
  107.             return true;
  108.     return false;
  109. }
  110.  
  111. bool grid::usedInBox(int digit, int row, int col) {
  112.     row = row - row % sectorSize;
  113.     col = col - col % sectorSize;
  114.     for(int sRow = 0; sRow < sectorSize; sRow++)
  115.         for(int sCol = 0; sCol < sectorSize; sCol++)
  116.             if(matrix[row+sRow][col+sCol] == digit)
  117.                 return true;
  118.     return false;
  119. }
  120. bool grid::isSafe(int digit, int row, int col) {
  121.     return !usedInRow(digit, row) && !usedInCol(digit, col) &&
  122.            !usedInBox(digit, row, col);
  123. }
  124.  
  125. bool grid::findSolution() {
  126.     grid *temp = new grid(*this);
  127.     temp->solve();
  128. }
  129.  
  130. bool grid::solve() {
  131.     int row, col;
  132.     if(!this->findUnassignedLocation(row,col)) {
  133.         this->output();
  134.         return true;
  135.     }
  136.     for(int digit = 1; digit <= this->gridSize; digit++)
  137.         if(this->isSafe(digit, row, col)) {
  138.             this->matrix[row][col] = digit;
  139.             if(this->solve())
  140.                 return true;
  141.             this->matrix[row][col] = 0;
  142.  
  143.         }
  144.     //delete &this;
  145.     return false;
  146. }
  147.  
  148.  
  149. bool grid::findUnassignedLocation(int &row, int &col) {
  150.     for(row = 0; row < gridSize; row++)
  151.         for(col = 0; col < gridSize; col++)
  152.             if(matrix[row][col] == 0)
  153.                 return true;
  154.     return false;
  155. }
  156.  
  157.  
  158.  
  159. //grid.txt
  160. 8 0 0 0 0 0 0 0 0
  161. 0 0 3 6 0 0 0 0 0
  162. 0 7 0 0 9 0 2 0 0
  163. 0 5 0 0 0 7 0 0 0
  164. 0 0 0 0 4 5 7 0 0
  165. 0 0 0 1 0 0 0 3 0
  166. 0 0 1 0 0 0 0 6 8
  167. 0 0 8 5 0 0 0 1 0
  168. 0 9 0 0 0 0 4 0 0
Advertisement
Add Comment
Please, Sign In to add comment