C program to find out if a solved Sudoku is valid or invalid.


Object of this program is to find out if a solved Sudoku is valid or invalid.

/* when the program encounters "break;" statement, execution will come out of the innermost loop in which it present. */

Program:

#include<stdio.h>
int main()
{
         int a[9][9];
         int i,j,sum,temp,flag=1;
         printf("\n enter input::\n");
         for(i=0;i<9;i++){
             for(j=0;j<9;j++){
                  scanf("%d",&temp);
                  if(temp>0 && temp<10)          /*   check for valid input for Sudoku   */
                  a[i][j]=temp;
                  else{                               /* if any element is not invalid then successive breaks  */
                        flag=0;                             
                        printf(" invalid input ");
                        break;
                      }
             }
             if(flag==0)
             break;
         }
          if(flag)             /* if every input is in valid range then go for validation of Sudoku  */
         {
               for(i=0;i<9;i++)
               {
                    sum=0;
                    for(j=0;j<9;j++)
                    sum=sum+a[i][j];
                    if(sum!=45)
                       break;            /*   if sum is not 45 in any row then  go out of for loop  */
               }
               if(sum==45)                /*  if sum is 45 in all rows then check for columns   */
               {
                     for(i=0;i<9;i++)
                    {
                          sum=0;
                          for(j=0;j<9;j++)
                          sum=sum+a[j][i];
                          if(sum!=45)           
                          break;        /*   if sum is not 45 in any column then  go out of for loop  */
                    }
                    if(sum==45)                              /* if sum is 45 in all columns too    */
                       printf("Valid");
                    else printf("invalid");
               }
               else
                   printf("\n Invalid");
         }
         return 0;
}


Sample input:

1 9 4 8 7 5 6 2 3
8 5 3 9 6 2 4 7 1
2 6 7 3 1 4 9 8 5
9 2 1 6 5 8 7 3 4
6 7 8 4 3 1 2 5 9
4 3 5 2 9 7 8 1 6
5 8 6 7 4 3 1 9 2
3 4 2 1 8 9 5 6 7
7 1 9 5 2 6 3 4 8

Output:

Valid
 

4 comments:

Ashok Pareek said...

No...your logic is not correct.
Why?
Here is an example:
5 3 4 1 2 6 7 8 9
3 4 5 2 1 7 8 9 6
4 5 6 7 3 2 9 1 8
1 8 3 4 5 9 6 2 7
2 7 9 5 8 3 4 6 1
7 2 1 6 9 8 3 4 5
6 9 2 8 7 1 5 3 4
9 6 8 3 4 5 1 7 2
8 1 7 9 6 4 2 5 3
Here sum of every element in a row and a column is 45...I have used numbers only 1,2,3,4,5,6,7,8,9 to form this..So the output according to your program is VALID for this sudoku...But everybody knows that it is invalid sudoku because here some of 3*3 matrices is not 45...After adding this condition please let me know the coding...

Unknown said...

Dear Sir,
In your program I can't understand the use of flag. Could you please help me to understand it?? please sir help me out..

Unknown said...

Ashok sir could you please help me by giving a correct program about this Sudoku game??

Chaithu003 said...

// C++ program to find minimum number of platforms
// required on a railway station
#include
#include
#include
using namespace std;
// Driver code
int check(int b[])
{
int x=0;
int c[10]={0};
for(int i=0;i<9;i++){
c[b[i]]++;
}
for(int i=0;i<10;i++)
{if(c[i]==1)x++;}

if(x==9)
return 1;
else
return 0;}
int main()
{
int a[9][9],y=0,s=0;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cin>>a[i][j];

}


}
for(int i=0;i<9;i++){
y=y+check(a[i]);

}

for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
s=s+a[i][j];

}


}


if(y==9&&s==405)
cout<<"True";
else
cout<<"False";


return 0;
}