Advertisement
tuixte

SudokuSolver

Dec 24th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. /*
  2.  * Sudoku Solver
  3.  * Michele Marolla
  4.  */
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9.  
  10. int sudoku[81][10];
  11. int unsolved = 0;
  12.  
  13. void printSudoku(){
  14.     for(int i=0; i<81; i++){
  15.         if(sudoku[i][0] == 1)
  16.             std::cout << sudoku[i][1] << " ";
  17.         else
  18.             std::cout << "x ";
  19.         if((i+1)%9 == 0)
  20.             std::cout << std::endl;
  21.     }
  22.     std::cout << std::endl;
  23. }
  24.  
  25. void solve(int *cell, int n){
  26.     if(cell[0] == 1) return;
  27.    
  28.     // Check row
  29.     int row = n/9;
  30.     for(int i=row*9; i<=row*9+8; i++){
  31.         if(sudoku[i][0] == 1)
  32.             cell[sudoku[i][1]] = -1;
  33.     }
  34.    
  35.    
  36.     // Check column
  37.     int col = n%9;
  38.     for(int i=col; i<=72+col; i=i+9){
  39.         if(sudoku[i][0] == 1)
  40.             cell[sudoku[i][1]] = -1;
  41.     }
  42.    
  43.     // Check rectangle
  44.     int startRow=0, startCol=0;
  45.    
  46.     if(row < 3) startRow = 0;
  47.     else if(row >= 3 && row < 6) startRow = 3;
  48.     else if(row >= 6) startRow = 6;
  49.    
  50.     if(col < 3) startCol = 0;
  51.     else if(col >= 3 && col < 6) startCol = 3;
  52.     else if(col >= 6) startCol = 6;
  53.    
  54.     int _n;
  55.     for(int i=0; i<3; i++){
  56.         for(int j=0; j<3; j++){
  57.             _n = (startRow + i) * 9 + startCol + j;
  58.             if(n == _n) continue;
  59.             if(sudoku[_n][0] == 1)
  60.                 cell[sudoku[_n][1]] = -1;
  61.         }
  62.     }
  63.    
  64.     // Final check
  65.     int choice = -1;
  66.     for(int i=1; i<10; i++){
  67.         if(cell[i] != -1){
  68.             if(choice == -1)
  69.                 choice = i;
  70.             else
  71.                 return;
  72.         }
  73.     }
  74.    
  75.     if(choice == -1){
  76.         std::cout << std::endl << "Unable to solve sudoku" << std::endl;
  77.         std::exit(1);
  78.     }
  79.    
  80.     cell[0] = 1;
  81.     cell[1] = choice;
  82.     unsolved--;
  83.    
  84. }
  85.  
  86. int main(){
  87.     std::cout << "Sudoku solver by Michele Marolla" << std::endl;
  88.    
  89.     // Initialize
  90.     std::cout << "Loading.." << std::endl;
  91.     for(int i=0; i<81; i++){
  92.         sudoku[i][0] = -1;
  93.         for(int j=1; j<10; j++){
  94.             sudoku[i][j] = j;
  95.         }
  96.     }
  97.    
  98.     // Read sudoku table
  99.     std::cout << "Opening sudoku..." << std::endl;
  100.     std::ifstream myfile;
  101.     myfile.open("sudoku.txt");
  102.     if(!myfile.is_open()){
  103.         std::cout << "Error: cannot open the file 'sudoku.txt'" << std::endl;
  104.         return 1;
  105.     }
  106.    
  107.    
  108.     std::string line;
  109.     for(int i=0; i<9; i++){
  110.         getline(myfile, line);
  111. #ifdef _WIN32
  112.         if(i!=0 && line.compare("\r") == 0)
  113.             getline(myfile, line);
  114. #endif
  115.         if(line.length() != 17)
  116.             return 1;
  117.        
  118.         std::string sub;
  119.         for(int j=0; j<17; j=j+2){
  120.             sub = line.substr(j,1);
  121.             if(sub == "x"){
  122.                 unsolved++;
  123.             }else{
  124.                 sudoku[9*i + j/2][0] = 1;
  125.                 sudoku[9*i + j/2][1] = atoi(sub.c_str());
  126.             }
  127.         }
  128.        
  129.     }
  130.     myfile.close();
  131.    
  132.     // Testing
  133.     printSudoku();
  134.    
  135.     // Solving
  136.     std::cout << std::endl << "Solving..." << std::endl;
  137.     while(unsolved > 0){
  138.         for(int i=0; i<81; i++)
  139.             solve(sudoku[i], i);
  140.     }
  141.    
  142.     // Output
  143.     std::cout << "Solved!" << std::endl;
  144.     printSudoku();
  145.    
  146.     // Quit
  147.     char x;
  148.     std::cout << "Press 'q' and then enter to quit: ";
  149.     std::cin >> x;
  150.    
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement