Solve the Sudoku | Practice | GeeksforGeeks

Given an incomplete Sudoku configuration in terms of a 9 x 9  2-D square matrix (grid[][]), the task to find a solved Sudoku. For simplicity, you may assume that there will be only one unique solution.

Sample Sudoku for you to get the logic for its solution:



Example 1:
Input:
grid[][] = 
[[3 0 6 5 0 8 4 0 0],
[5 2 0 0 0 0 0 0 0],
[0 8 7 0 0 0 0 3 1 ],
[0 0 3 0 1 0 0 8 0],
[9 0 0 8 6 3 0 0 5],
[0 5 0 0 9 0 6 0 0],
[1 3 0 0 0 0 2 5 0],
[0 0 0 0 0 0 0 7 4],
[0 0 5 2 0 6 3 0 0]]
Output:
3 1 6 5 7 8 4 9 2
5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1
2 6 3 4 1 5 9 8 7
9 7 4 8 6 3 1 2 5
8 5 1 7 9 2 6 4 3
1 3 8 9 4 7 2 5 6
6 9 2 3 5 1 8 7 4
7 4 5 2 8 6 3 1 9


Your Task:
You need to complete the two functions:
SolveSudoku(): Takes a grid as its argument and returns true if a solution is possible and false if it is not.
printGrid(): Takes a grid as its argument and prints the 81 numbers of the solved Sudoku in a single line with space separation.
NOTE: Do not give a new line character after printing the grid. It has already been taken care of in the Driver Code.

Expected Time Complexity: O(9N*N).
Expected Auxiliary Space: O(N*N).

Constraints:
0 ≤ grid[i][j] ≤ 9
+1
omsingh44594 days ago
bool isSafe(int i, int j, int n, int grid[N][N]){       for(int k=0; k<N; k++){           if(grid[i][k]==n  || grid[k][j]==n){               return false;           }       }       int s= sqrt(N);       int cs = j-j%s;       int rs = i-i%s;       for(int p=0; p<s; p++){           for(int q=0; q<s; q++){               if(grid[p+rs][q+cs]==n){                   return false;               }           }       }       return true;   }   bool SolveSudoku(int grid[N][N])     {        // Your code here       int i=0;       int j=0;       bool flag = false;       for(i=0; i<N; i++){           for( j=0; j<N; j++){               if(grid[i][j]==0){                   flag = true;                   break;               }           }           if(flag){               break;           }       }       if(i==N && j==N){           return true;       }       for(int n=1; n<=N; n++){           if(isSafe(i, j, n, grid)){               grid[i][j]=n;               if(SolveSudoku(grid)==true){                   return true;               }               grid[i][j]=0;           }       }       return false;   }
-7
namanchandak6 days ago
super gajab khatarnak dharti faard solution
 
 
Test Cases Passed:
150 / 150
Total Points Scored:
8/8
Total Time Taken:
0.07/1.1
Your Accuracy:
100%
Attempts No.:
1
 
 
class Solution 
{
    public:
    //this is okay
    bool SolveSudoku(int grid[N][N])
    {
        return Sol(grid);
    }
    
    
    //to check 
     bool isvalid(int grid[N][N],int row,int col,int  c)
    {
        for(int i=0;i<9;i++)
        {
            if(grid[i][col]==c)
                return false;
            if(grid[row][i]==c)
                return false;
            
            if(grid[3*(row/3)+i/3][3*(col/3)+i%3]==c)
                return false;
            
        }
        
        
        return true;
        
    }
    //Function to find a solved Sudoku. 
    bool Sol(int grid[N][N])  
    { 
        // Your code here
        
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                if(grid[i][j]==0)
                {
                    for(int c=1;c<=9;c++)
                    {
                        if(isvalid(grid,i,j,c))
                        {
                            grid[i][j]=c;
                        
                        if(Sol(grid)==true)
                            return true;
                        else
                           grid[i][j]=0; 
                        
                        }
                    }
                    return false;
                }
                
            }
        }
        
        return true;
    }
    
    //Function to print grids of the Sudoku.
    
    //this is okay
    void printGrid (int grid[N][N]) 
    {
        // Your code here 
        
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                cout<<grid[i][j]<<" ";
    }
        }
    }
};
0
kvatsadeo1 week ago
 public:
    //Function to find a solved Sudoku. 
    bool issafe(int grid[N][N],int i,int j,int k){
        for(int c=0;c<9;c++){
            if(grid[i][c]==k) return false;
        }
        for(int r=0;r<9;r++){
            if(grid[r][j]==k) return false;
        }
        int rs=(i/3)*3;
        int cs=(j/3)*3;
        for(int r=0;r<3;r++){
            for(int c=0;c<3;c++){
                if(grid[rs+r][cs+c]==k) return false;
            }
        }
        return true;
    }
    bool solve(int grid[N][N],int i,int j){
        if(i==N) return true; 
        if(j==N){
            i++;
            j=0;
        }
        if(grid[i][j]>0) return solve(grid,i,j+1);
        for(int k=1;k<=9;k++){
            if(issafe(grid,i,j,k)){
                grid[i][j]=k;
                if(solve(grid,i,j+1)) return true;
                grid[i][j]=0;
            }
        }
        return false;
    }
    bool SolveSudoku(int grid[N][N])  
    { 
         return solve(grid,0,0);
        
    }
    
    //Function to print grids of the Sudoku.
    void printGrid (int grid[N][N]) 
    {
        // Your code here 
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                cout<<grid[i][j]<<" ";
            }
        }
    }
-1
abhishekguptaaa2 weeks ago
C++ SOLUTION:
 
    bool issafe(int grid[][N], int val, int row, int col)   {                for(int i=0;i<9;i++)         {             if(grid[row][i]==val)             {                 return false;             }             if(grid[i][col]==val)             {                 return false;             }             if(grid[3*(row/3) + i/3 ][3*(col/3) + i%3]==val)             {                 return false;             }         }                          return true;                          }                  bool solve(int grid[][N])   {                                    for(int i=0;i<9;i++)         {             for(int j=0;j<9;j++)             {                 if(grid[i][j]==0)                 {                                          for(int val=1;val<=9;val++)                     {                                                  if(issafe(grid,val,i,j))                         {                             grid[i][j]=val;                                                                           bool nextsolve=solve(grid);                                                  if(nextsolve==true)                         {                             return true;                         }                         else                         {                             grid[i][j]=0;                         }                                                  }                                              }                                          return false;                 }             }         }                                    return true;                        }         bool SolveSudoku(int grid[N][N])     {                return solve(grid);   }      //Function to print grids of the Sudoku.   void printGrid (int grid[N][N])    {       for(int i=0;i<9;i++)       {           for(int j=0;j<9;j++)           {               cout<<grid[i][j]<<" ";           }           
       }   }};
0
rajdeep_awsm2 weeks ago
// { Driver Code Starts#include <bits/stdc++.h>using namespace std;// UNASSIGNED is used for empty cells in sudoku grid #define UNASSIGNED 0  
// N is used for the size of Sudoku grid.  // Size will be NxN  #define N 9  
// } Driver Code Endsclass Solution {   public:   //Function to find a solved Sudoku.      bool isSafe(int val,int row,int col,int grid[N][N])   {       for(int i=0;i<9;i++)       {           for(int j=0;j<9;j++)           {               if(grid[row][j]==val)               {                   return false;               }                              if(grid[i][col] == val)               {                   return false;               }                              if(grid[3*(row/3) + i/3][3*(col/3)+j%3] == val)               {                   return false;               }           }       }              return true;   }         bool fun(int grid[N][N])   {       for(int i=0;i<9;i++)       {           for(int j=0;j<9;j++)           {               if(grid[i][j]==0)               {                   for(int val =1;val<=9;val++)                   {                       if(isSafe(val,i,j,grid))                       {                           grid[i][j] = val;                           bool f1 = fun(grid);                           if(f1==true)                           {                               return true;                           }                                                      grid[i][j] = 0;                       }                   }                                      return false;               }           }       }              return true;          }      bool SolveSudoku(int grid[N][N])     {        return fun(grid);          }      //Function to print grids of the Sudoku.   void printGrid (int grid[N][N])    {       // Your code here               for(int i=0;i<9;i++)       {           for(int j=0;j<9;j++)           {               cout<<grid[i][j]<<" ";           }       }   }};
// { Driver Code Starts.
int main() {int t;cin>>t;while(t--){ int grid[N][N];  for(int i=0;i<9;i++)     for(int j=0;j<9;j++)         cin>>grid[i][j];          Solution ob;  if (ob.SolveSudoku(grid) == true)             ob.printGrid(grid);         else           cout << "No solution exists";                cout<<endl;}return 0;}  // } Driver Code Ends
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.