jayati

Solve the Sudoku

Oct 4th, 2023
1,023
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 1 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. // UNASSIGNED is used for empty cells in sudoku grid
  5. #define UNASSIGNED 0  
  6.  
  7. // N is used for the size of Sudoku grid.  
  8. // Size will be NxN  
  9. #define N 9  
  10.  
  11.  
  12. // } Driver Code Ends
  13. class Solution
  14. {
  15.     public:
  16.     //Function to find a solved Sudoku.
  17.     bool isSafe(int grid[N][N],int row,int col,int val)
  18.     {
  19.         for(int i=0;i<N;i++)
  20.         {
  21.             if(grid[row][i]==val)
  22.             {
  23.                 return false;
  24.             }
  25.             if(grid[i][col]==val)
  26.             {
  27.                 return false;
  28.             }
  29.             if(grid[3*(row/3)+i/3][3*(col/3)+i%3]==val)
  30.             {
  31.                 return false;
  32.             }
  33.         }
  34.         return true;
  35.     }
  36.     bool SolveSudoku(int grid[N][N])  
  37.     {
  38.         // Your code here
  39.         for(int row=0;row<N;row++)
  40.         {
  41.             for(int col=0;col<N;col++)
  42.             {
  43.                 if(grid[row][col]==0)
  44.                 {
  45.                     for(int k=1;k<=9;k++)
  46.                     {
  47.                         if(isSafe(grid,row,col,k))
  48.                         {
  49.                             grid[row][col]=k;
  50.                             int solpossible = SolveSudoku(grid);
  51.                             if(solpossible)
  52.                             {
  53.                                 return true;
  54.                             }
  55.                             else
  56.                             {
  57.                                  grid[row][col]=0;
  58.                             }
  59.                            
  60.                            
  61.                         }
  62.                     }
  63.                     return false;
  64.                 }
  65.             }
  66.         }
  67.         return true;
  68.     }
  69.    
  70.     //Function to print grids of the Sudoku.
  71.     void printGrid (int grid[N][N])
  72.     {
  73.         // Your code here
  74.         SolveSudoku(grid);
  75.         for(int i=0;i<N;i++)
  76.         {
  77.             for(int j=0;j<N;j++)
  78.             {
  79.                 cout<<grid[i][j]<<" ";
  80.             }
  81.             // cout<<endl;
  82.         }
  83.         // cout<<endl;
  84.     }
  85. };
  86.  
  87. //{ Driver Code Starts.
  88.  
  89. int main() {
  90.     int t;
  91.     cin>>t;
  92.     while(t--)
  93.     {
  94.         int grid[N][N];
  95.        
  96.         for(int i=0;i<9;i++)
  97.             for(int j=0;j<9;j++)
  98.                 cin>>grid[i][j];
  99.                
  100.         Solution ob;
  101.        
  102.         if (ob.SolveSudoku(grid) == true)  
  103.             ob.printGrid(grid);  
  104.         else
  105.             cout << "No solution exists";  
  106.        
  107.         cout<<endl;
  108.     }
  109.    
  110.     return 0;
  111. }
  112. // } Driver Code Ends
Advertisement
Comments
Add Comment
Please, Sign In to add comment