Search This Blog

C Program to Validate Sudoku - Checking Correctness of Sudoku

Here we shall see a c program to validate sudoku a sudoku. The c program i have written here checks the correctness of a sudoku. The programs can be used to check sudoku solution for 9x9 sudoku. First of all, we should know what are the constraints.
  • Each row should contain all numbers from 1 to 9 (inclusive)
  • Each column should contain all numbers from 1 to 9 (inclusive)
  • The 3x3 squares should also contain all numbers from 1 to 9 (inclusive)
If all above conditions are satisfied, the sudoku is correct. The c program to check whether a solution for a sudoku is correct is as follows:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

void report(char *s,int i,int j)
{
printf("\nThe sudoku is INCORRECT");
printf("\nin %s. Row:%d,Column:%d",s,i+1,j+1);
getch();
exit(0);
}

void main()
{
int i,j,a[9][9];
char c;
int si,sj,flag;
printf("\nEnter the sudoku");
/*
Going to read sudoku matrix (9x9).
Since numbers 1 to 9 are single digit,
it is enough to read them as char.
To convert them from their ascii to int,
subtract ascii of '0' from the character.
*/
for(i=0;i<9;i++)
    for(j=0;j<9;j++)
    {
    scanf("%c",&c);
    a[i][j]=c-'0';
    }
/*++++++++++++++++++
checking rows
+++++++++++++++++++
we check each cell in each row.
We start with a flag 0x0000.
if 1 is found zeroth bit of flag is set.
if 2 is found, first bit is set
and so on.
If all digits 1 to 9 are present, flag's value
will be 0x01FF.
If flag is 0x01FF after traversing a row,
the row has all numbers 1 to 9.
So, it is correct.
If the flag is not 0x01FF after traversing a row,
the row is incorrectly filled.
Then we call report() function
*/
for(i=0;i<9;i++)
    {
    flag=0x0000;
    for(j=0;j<9;j++)
        flag|=1<<(a[i][j]-1);
    if(flag!=0x01FF)
        report("row",i,j-1);
    }

/*++++++++++++++++++
checking columns
+++++++++++++++++++
Just like row checking.
The flag is for a column.
*/
for(j=0;j<9;j++)
    {
    flag=0x0000;
    for(i=0;i<9;i++)
        flag|=1<<(a[i][j]-1);
    if(flag!=0x01FF)
        report("col",i-1,j);
    }
/*++++++++++++++++++
checking Squares (3x3)
+++++++++++++++++++
Just like row checking.
The flag is for a square.
*/
for(si=0;si<3;si++)
    {
    for(sj=0;sj<3;sj++)
        {
        flag=0x0000;
        for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                flag|=1<<(a[si*3+i][sj*3+j]-1);
            }         if(flag!=0x01FF)                 report("square",si*3+i-1,sj*3+j-1);         }     } printf("\nThe sudoku is correct"); }



To test the program, you may try the following inputs. You may copy paste a line from below sudoku solutions to the program. (Each line is a correct solution)


123456789759183426648297315374915268896372154512864973931528647265749831487631592
123456789857923164964817235386241597275639841491578623538764912642195378719382456
123456789759283614486971253342768591891542367567319428935127846278694135614835972
123456789849173256765298431586941372291735648437862915352617894978524163614389527
123456789784913652569827134316598247845271963297634815652149378938765421471382596
123456789496817235587329461864275193359184627712693854631942578978561342245738916
123456789746198532859732146298314675374569821615287394967845213481623957532971468
123456789867239145954781362392567418416928573578143926745692831281375694639814257
123456789475981362869327541654813927791642853382795614938164275516279438247538196
123456789968137524457892136892374651716285493534619872685943217279561348341728965
123456789645987312897312645972863451536124897418579236364291578289735164751648923

No comments: