Advertisement
adambkehl

Sudoku - 2/12/19

Feb 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. //sudoku.h
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. using namespace std;
  7. #define SIZE 9
  8.  
  9. class Sudoku {
  10. private:
  11.     int **grid;
  12. public:
  13.     Sudoku();
  14.     ~Sudoku();
  15.     void genPuzzleFromFile(char *fname);
  16.     void printPuzzle();
  17.     bool const isValidRow(int x, int y, int value);
  18.     bool const isValidCol(int x, int y, int value);
  19.     bool const isValidBox(int x, int y, int value);
  20.     bool const isValid(int x, int y, int value);
  21.     bool const solve();
  22. };
  23.  
  24.  
  25.  
  26.  
  27. //main.cpp
  28. int main(int argc, char *argv[])
  29. {
  30.     Sudoku puzzle;
  31.     /*
  32.     if (argc == 1)
  33.         puzzle.genPuzzle(1);
  34.     else*/
  35.  
  36.     puzzle.genPuzzleFromFile(argv[1]);
  37.  
  38.     puzzle.printPuzzle();
  39.     puzzle.solve();
  40.     puzzle.printPuzzle();
  41.     cin.get(), cin.get();
  42. }
  43.  
  44.  
  45.  
  46.  
  47. //sudoku.cpp
  48. Sudoku::Sudoku() {
  49.     grid = new int*[SIZE];
  50.     for (int i = 0; i < SIZE; i++) grid[i] = new int[SIZE];
  51.     for (int x = 0; x < SIZE; x++)
  52.         for (int y = 0; y < SIZE; y++)
  53.             grid[x][y] = 0;
  54. }
  55.  
  56. Sudoku::~Sudoku() {
  57.     for (int i = 0; i < SIZE; i++) delete[] grid[i];
  58.     delete[] grid;
  59. }
  60.  
  61. void Sudoku::genPuzzleFromFile(char *fname) {
  62.     ifstream inputFile;
  63.     inputFile.open(fname);
  64.     for (int i = 0; i < SIZE; i++)
  65.         for (int j = 0; j < SIZE; j++)
  66.             inputFile >> grid[i][j];
  67.     inputFile.close();
  68. }
  69. //void Sudoku::genPuzzle(int level) {}
  70. void Sudoku::printPuzzle() {
  71.     cout << "------------------\n";
  72.     for (int i = 0; i < SIZE; i++) {
  73.         for (int j = 0; j < SIZE; j++) {
  74.             cout << "|" << grid[i][j];
  75.         }
  76.         cout << "|" << endl;
  77.     }
  78.     cout << "------------------\n";
  79. }
  80. bool const Sudoku::isValidRow(int x, int y, int value) {
  81.     for (int i = 0; i < SIZE; i++) {
  82.         if (y != i && grid[x][i] == value)
  83.             return false;
  84.     }
  85.     return true;
  86. }
  87. bool const Sudoku::isValidCol(int x, int y, int value) {
  88.     for (int i = 0; i < SIZE; i++) {
  89.         if (x != i && grid[i][y] == value)
  90.             return false;
  91.     }
  92.     return true;
  93. }
  94.  
  95. bool const Sudoku::isValidBox(int x, int y, int value) {
  96.     int box, row, col;
  97.    
  98.     row = (x / 3) * 3;
  99.     col = (y / 3) * 3;
  100.     for (int i = row; i < row + 3; i++) {
  101.         for (int j = col; j < col + 3; j++) {
  102.             if (i != x && j != y)
  103.                 if (grid[i][j] == value)
  104.                     return false;
  105.         }
  106.     }
  107.     return true;
  108. }
  109. bool const Sudoku::isValid(int x, int y, int value) {
  110.     if (isValidRow(x, y, value) &&
  111.         isValidCol(x, y, value) &&
  112.         isValidBox(x, y, value))
  113.         return true;
  114.     return false;
  115. }
  116. bool const Sudoku::solve() {
  117.     for (int i = 0; i < SIZE; i++) {
  118.         for (int j = 0; j < SIZE; j++) {
  119.             if (grid[i][j] == 0) {
  120.                 for (int k = 1; k <= SIZE; k++) {
  121.                     if (isValid(i, j, k)) {
  122.                         grid[i][j] = k;
  123.                         if (solve()) return true;
  124.                         else grid[i][j] = 0;
  125.                     }
  126.                 }
  127.                 return false;
  128.             }
  129.         }
  130.     }
  131.     return true;
  132. }
  133.  
  134. /* Puzzle.txt
  135. 0 0 3 0 2 0 6 0 0
  136. 9 0 0 3 0 5 0 0 1
  137. 0 0 1 8 0 6 4 0 0
  138. 0 0 8 1 0 2 9 0 0
  139. 7 0 0 0 0 0 0 0 8
  140. 0 0 6 7 0 8 2 0 0
  141. 0 0 2 6 0 9 5 0 0
  142. 8 0 0 2 0 3 0 0 9
  143. 0 0 5 0 1 0 3 0 0
  144. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement