Advertisement
Guest User

Untitled

a guest
May 27th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <string>
  6. #include <set>
  7.  
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int b[9][9];//the sudoku grid
  13. set<int >rows[9];//to know which numbers appears in each row
  14. set<int >columns[9];//to know which numbers appears in each column
  15. set<int >blocks[9];//to know which numbers appears in each 3*3 block
  16.  
  17. int itr=0;//to count how many iterations have been made
  18.  
  19. int getBlock(int i,int j){//to get the block number by the row and column index
  20.     if(i<3){
  21.         if(j<3)return 0;
  22.         else if(j<6)return 1;
  23.         else return 2;
  24.  
  25.     }else if(i<6){
  26.         if(j<3)return 3;
  27.         else if(j<6)return 4;
  28.         else return 5;
  29.     }else{
  30.         if(j<3)return 6;
  31.         else if(j<6)return 7;
  32.         else return 8;
  33.     }
  34. }
  35.  
  36. bool solve(int i,int j){
  37.     //to print the number of iterations made
  38.     itr++;
  39.     if(itr%100000==0)cout<<itr<<endl;
  40.    
  41.     if(j==9){//to move to the next row
  42.         j=0;
  43.         i++;
  44.     }
  45.    
  46.    
  47.     if(i==9)return true;//which means we have finished the grid
  48.  
  49.     if(b[i][j]!=0)return solve(i,j+1);//if the cell isn't empty move to the next cell
  50.    
  51.     for(int v=1;v<=9;v++){//try every possible number 1->9
  52.        
  53.         if(rows[i].count(v)>0 || columns[j].count(v)>0 || blocks[getBlock(i, j)].count(v)>0)//continue if the numbers appears in the same row, column or block
  54.             continue;
  55.        
  56.         b[i][j]=v;//try the number
  57.         rows[i].insert(v);//put the number in the ith row
  58.         columns[j].insert(v);//put the number in the jth row
  59.         blocks[getBlock(i, j)].insert(v);//put the number in the correct block
  60.        
  61.         bool tr=solve(i,j+1);//try to move to the next cell with this number
  62.  
  63.         if(tr)return tr;//means that we have reached the last cell and returned true
  64.  
  65.         //if we did't return true
  66.         //erase the number from the row ,column and block
  67.         rows[i].erase(v);
  68.         columns[j].erase(v);
  69.         blocks[getBlock(i, j)].erase(v);
  70.         //try next number
  71.     }
  72.     //we did't find any suitable number so return false
  73.     b[i][j]=0;
  74.     return false;
  75.  
  76.  
  77. }
  78.  
  79. int main() {
  80.  
  81.     cin.tie(0);
  82.     cin.sync_with_stdio(0);
  83.  
  84.     //read the numbers
  85.     for(int i=0;i<9;i++){
  86.         for(int j=0;j<9;j++){
  87.             cin>>b[i][j];
  88.             rows[i].insert(b[i][j]);
  89.             columns[j].insert(b[i][j]);
  90.             blocks[getBlock(i,j)].insert(b[i][j]);
  91.         }
  92.     }
  93.  
  94.     //solve it from first cell
  95.     bool s=solve(0,0);
  96.     if(s){//means we found an valid solution
  97.         for(int i=0;i<9;i++){
  98.             for(int j=0;j<9;j++){
  99.                 string sep=" ";
  100.                 if(j%3==2)sep=" | ";
  101.                 cout<<b[i][j]<<sep;
  102.             }
  103.             cout<<endl;
  104.             if(i%3==2)cout<<"____________________\n";
  105.         }
  106.     }
  107.     else{//sadly we did't find a solution
  108.         cout<<":(";
  109.  
  110.     }
  111.  
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement