Advertisement
permatrash

Sudoku solver main.cpp

Jul 14th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<stack>
  4. #include"cell.h"
  5. #include"puzzle.h"
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, char* argv[]){
  10.  
  11.     Puzzle puzzle;
  12.     Puzzle curPuzzle;
  13.     Puzzle pushPuzzle;
  14.     stack<Puzzle> altStack;
  15.  
  16.     string input = "54.2.9..1...5....4..7...9..8...3..67...6.5...93..1...2..1...6..2....6...3..1.7.49";
  17.  
  18.  
  19.     //Validate Input======================================================================================================
  20.     if(input.length() != 81){
  21.         cout <<"INVALID INPUT -- not 81 chars" << endl;;
  22.         return 1;
  23.     }
  24.  
  25.     for(unsigned int i=0; i<input.length(); i++){
  26.  
  27.         if(input[i] != '.' && input[i] != '1' && input[i] != '2' && input[i] != '3' && input[i] != '4' && input[i] != '5' &&
  28.         input[i] != '6' && input[i] != '7' && input[i] != '8' && input[i] != '9' ){
  29.  
  30.             cout << "INVALID INPUT - CHARACTER " << input[i] << " at location " << i << endl;
  31.             return 1;
  32.         }
  33.     }
  34.  
  35.     //Set up initial puzzle========================================================================================================
  36.  
  37.     for(int i=0; i<9; i++){
  38.         for(int j=0;j<9;j++){
  39.  
  40.             //row and column
  41.             puzzle.board[i][j].setRow(i);
  42.             puzzle.board[i][j].setColumn(j);
  43.  
  44.             if(input.at((i*9)+j) == '.'){
  45.                 //set . to 0
  46.                 puzzle.board[i][j].setValue(0);
  47.  
  48.                 //set candidates to 1s, because they are initilized as zero.
  49.                 for(int k=1; k <=9; k++){
  50.                     puzzle.board[i][j].setCandidate(k,1);
  51.                 }
  52.             }else{
  53.                 //otherwise set cell value and eliminate its candidates
  54.  
  55.                 puzzle.board[i][j].setValue(input.at((i*9)+j) - 48 );
  56.             }
  57.         }
  58.     }
  59.  
  60. //puzzle.eliminateCandidates();
  61. //puzzle.testRead();
  62.  
  63.     //Push initial board onto stack================================================================================
  64.     altStack.push(puzzle);
  65.  
  66.     while(!altStack.empty()){
  67.         curPuzzle = altStack.top();
  68.         altStack.pop();
  69.  
  70.         //Initial candidate elimination===============================================================================================
  71.         curPuzzle.eliminateCandidates();
  72.  
  73. //curPuzzle.testRead();
  74.  
  75.         //Naked SIngles==============================================================================
  76.  
  77.         while(curPuzzle.findSingleCandidate() == 1){
  78.  
  79.                 puzzle.eliminateCandidates();
  80.         }
  81.  
  82. //cout << "The current puzzle after elimination is: " << endl;
  83. //curPuzzle.testRead();
  84.  
  85.         //Guess Solver
  86.         while(curPuzzle.isSolved() == 0){
  87.  
  88.         int i=0,j=0,k=0;
  89.  
  90.             //Find first empty cell
  91.             for(i=0; i<9; i++){
  92.                 for(j=0; j<9; j++){
  93.                     if(curPuzzle.board[i][j].getValue() == 0) break;
  94.                 }
  95.  
  96.                 if( (j<9) && curPuzzle.board[i][j].getValue() == 0) break;
  97.             }
  98.  
  99. //            //if there are no empty cells and no conflicts then puzzle is sovled  ---  MAY BE NOT NEEDED
  100. //            if( (i==9) && (j==9) && (curPuzzle.isSolved() == 1) ){
  101. //                cout << "SOLUTION FOUND: " << endl;
  102. //                for(int i=0; i<9; i++){
  103. //                    for(int j=0;j<9;j++){
  104. //                        cout << curPuzzle.board[i][j].getValue();
  105. //                    }
  106. //                }
  107. //                break;
  108. //            }
  109.  
  110. //cout << "First Empty Cell: [" << i << "][" << j << "]" << " with candidates ";
  111. //for(k=1; k<=9;k++){
  112. //    cout << curPuzzle.board[i][j].getCandidate(k);
  113. //}
  114. //cout << endl;
  115.  
  116.             //find first valid candidate
  117.             for(k=1; k<=9;k++){
  118.                 if(curPuzzle.board[i][j].getCandidate(k) == 1) break;
  119.             }
  120.  
  121. //cout << "First valid candidate is " << k << endl;
  122.  
  123.             //if it runs through all candidates, scrap the board and go back one on the stack
  124.             if(k == 10) break;
  125.  
  126.             //Set the guessed candidate to the cell
  127.             curPuzzle.board[i][j].setValue(k);
  128.  
  129.             //check for board conflicts
  130.             if(curPuzzle.checkConflicts(i,j) == 1){
  131.                 //set cell as a zero again
  132.                 curPuzzle.board[i][j].setValue(0);
  133.  
  134.                 //Remove canddidate from possible candidates
  135.                 curPuzzle.board[i][j].setCandidate(k,0);
  136.  
  137. //cout << "CONFLICT DETECTED - RESET CELL [" << i << "][" << j <<"] to 0" << " from " << k << endl;
  138.             }else{
  139. //cout << "Cell [" << i << "][" << j << "] set as candidate " << k << endl;
  140.  
  141.                 //Remove canddidate from possible candidates
  142.                 curPuzzle.board[i][j].setCandidate(k,0);
  143.  
  144.                 //Push current board onto stack with a reset value
  145.                 pushPuzzle = curPuzzle;
  146.                 pushPuzzle.board[i][j].setValue(0);
  147.  
  148.                 altStack.push(pushPuzzle);
  149.             }
  150.  
  151.             if(curPuzzle.isSolved() == 1){
  152.                 cout << "SOLUTION FOUND: " << endl;
  153.                 for(int i=0; i<9; i++){
  154.                     for(int j=0;j<9;j++){
  155.                         cout << curPuzzle.board[i][j].getValue();
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.  
  161.     }
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement