Program to Check Whether Given Sudoku Solution Valid or Not

Program to Check Whether Given Sudoku Solution Valid or Not

This program will Check whether the given Sudoku Solution is Valid Or Not .


Input :

 


Output :


Logic To Check for Valid Solution of Sudoku :

    • Get the Sudoku Solution from the user and store them in 2-Dimensinal integer Array .
    • Using another integer array store the count of each digits entered .
    • If the entered number is not between 1 to 9 again ask for the input .
    • A Valid Sudoku must satisfies the Following Conditions :
      1. Count of each digit entered must be Exactly 9 .
      2. All the 9 Rows and 9 Column Elements Sum should be Exactly 45 .
      3. Also the 9 , 3×3 Sub-matrix Sum of Elements Should be Exactly 45 .
    • The Reason for 45 is Sum of 1 to 9 is 45 . (1+2+3+4+5+6+7+8+9=45)
    • If the given Sudoku Solution Satisfies the Above three conditions then the Given Sudoku Solution is Valid .
    • If the Sudoku Solution fails to Satisfies any one of above three Conditions then the Solution is Invalid .

Program :

#include<iostream>
using namespace std;
int main()
{
  int a[9][9],c[9];			//a=sudoku_values,c=count_of_each_numbers
  int w=0;			        //w=used as flag variable
  cout<<"Enter the Sudoku Values One-By-One :";
  for(int i=0;i<9;i++)	      //this for loop is used to get sudoku values
  {
    for(int j=0;j<9;j++)
    {
      INPUT:
      char t;
      cin>>t;
      int temp=t-48;
      if(temp>0&&temp<10)     //if number is not between 1 to 9 then again number is asked
      {
        a[i][j]=temp;
        switch(temp)          //this switch statement counts each digits entered and store them in c[]
        {
          case 1:
                c[temp-1]=c[temp-1]+1;
                break;
          case 2:
                c[temp-1]=c[temp-1]+1;
                break;
          case 3:
                c[temp-1]=c[temp-1]+1;
                break;
          case 4:
                c[temp-1]=c[temp-1]+1;
                break;
          case 5:
                c[temp-1]=c[temp-1]+1;
                break;
          case 6:
                c[temp-1]=c[temp-1]+1;
                break;
          case 7:
                c[temp-1]=c[temp-1]+1;
                break;
          case 8:
                c[temp-1]=c[temp-1]+1;
                break;
          case 9:
                c[temp-1]=c[temp-1]+1;
                break;
        }
      }
      else
      {
        cout<<"Enter Between 1-10 !";
        goto INPUT;
      }
    }
  }
  //if all the digits entered is not have exact count of 9
  //then the sudoku is not valid
  for(int i=0;i<9;i++)
  {
    if(c[i]!=9)
    {
      cout<<"Sudoku is Invalid!";
      w=1;
      break;
    }
  }
  //the following for loop is used to count sum of both
  //horizontal and vertical direction
  if(w==0)
  {
    for(int i=0;i<9;i++)
    {
      int sh=0,sv=0;    //sh=sum_horizontal,sv=sum_vertical
      for(int j=0;j<9;j++)
      {
        sh=sh+a[i][j];
        sv=sv+a[j][i];
      }
      if(sh!=45||sv!=45)    //if sum is not exactly 45 then sudoku is invalid
      {
        cout<<"Sudoku is Invalid!";
        w=1;
        break;
      }
    }
  }
  //this following for loops finds the sum of all 3x3 sub matrices
  if(w==0)
  {
    for(int vc=0;vc<3;vc++)
    {
      for(int hc=0;hc<3;hc++)
      {
        int ssm=0;     //ssm=sum_sub_matrix
        for(int i=0;i<3;i++)
        {
          for(int j=0;j<3;j++)
          {
            ssm=ssm+a[(i+(vc*3))][(j+(hc*3))];
          }
          //cout<<"\n";
        }
        if(ssm!=45)      //if submatrix sum is not exactly 45 then sudoku is not valid
        {
          cout<<"Sudoku is Invalid!";
          w=1;
          break;
        }
      }
    }
  }
  //if value of flag w is unchanged then it satisfies all condition
  //so sudoku is valid
  if(w==0)       
  {
    cout<<"Sudoku is Valid !";
  }
  return 0;
}

 

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Suriya27

A Programmer who have his programming skills on C,C++, Java and also interested in Android Development. He follows the famous quotes by Linus Torvalds ” Talk is cheap, Show me the code “ .
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x