Sunday, December 27, 2009

Sudoku Solver


// Sudoku.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


 


 


int _tmain(int argc, _TCHAR* argv[])


{


      int A[100][100],r,c,inr,inc,used[10],count,trav,i,j,num=0,a,b;


      printf("Enter the values of the elements already present:\n");


      printf("Enter 0 for empty positions:\n");


      //Taking the input question.


      for(i=0;i<9;i++)


      {


            for(j=0;j<9;j++)


            {


                  scanf("%d",&A[i][j]);


                  if(A[i][j]!=0)


                        num++;


            }


      }


      //Printing the input matrix.


      printf("\nThe input matrix is:\n");


      for(i=0;i<9;i++)


      {


            for(j=0;j<9;j++)


            {


                  printf("%3d",A[i][j]);


            }


            printf("\n");


      }


      //Logic for finding the answer matrix.


      while(num<81)


      {


            for(i=0;i<9;i++)//Determining the row of the empty position.


            {


                  for(j=0;j<9;j++)//Determining the column of the empty position.


                  {


                        if(A[i][j]==0)//Condition checking whether the position is empty.


                        {


                              for(count=0;count<9;count++)


                                    used[count]=0;//Array to determine which number is already present.


                              count=9;


                              //Row check.


                              a=i;


                              for(b=0;b<9;b++)


                              {


                                    if(b!=j && A[a][b]!=0)


                                    {


                                          used[A[a][b]-1]=1;


                                    }


                              }


                              //Column check.


                              a=j;


                              for(b=0;b<9;b++)


                              {


                                    if(b!=i && A[b][a]!=0)


                                    {


                                          used[A[b][a]-1]=1;


                                    }


                              }


                              //Sub-square check.


                              if(i<=2)


                              {r=3;inr=0;}


                              else if(i<=5)


                              {r=6;inr=3;}


                              else if(i<=8)


                              {r=9;inr=6;}


                              if(j<=2)


                              {c=3;inc=0;}


                              else if(j<=5)


                              {c=6;inc=3;}


                              else if(j<=8)


                              {c=9;inc=6;}


                              //Checking in the sub-square determined by the above conditions.


                              for(a=inr;a<r;a++)


                              {


                                    for(b=inc;b<c;b++)


                                    {


                                          if(a!=i || b!=j || A[a][b]!=0)


                                          {


                                                used[A[a][b]-1]=1;


                                          }


                                    }


                              }


                              //Counting the number of elements permitted to come at the empty position.


                              for(trav=0;trav<9;trav++)


                              {


                                    if(used[trav]==1)


                                    {count--;}


                              }


                              if(count==1)


                              {


                                    for(trav=0;trav<9;trav++)


                                    {


                                          if(used[trav]==0)


                                                break;


                                    }


                                    A[i][j]=trav+1;//Element to be put=(Index of the zero element)+1.


                                    num++;


                              }


                        }


                  }


            }


      }


      //Printing the answer matrix.


      printf("\nThe answer is:\n");


      for(i=0;i<9;i++)


      {


            for(j=0;j<9;j++)


            {


                  printf("%3d",A[i][j]);


            }


            printf("\n");


      }


      return 0;


}


 


Enter the values of the elements already present:
Enter 0 for empty positions:
0 0 6 5 0 0 0 0 0
8 0 9 1 0 4 0 7 0
0 0 3 0 0 0 2 4 0
0 8 7 0 0 3 0 6 5
0 9 0 7 0 0 0 1 0
4 0 1 0 0 9 7 8 0
0 6 0 0 8 0 1 0 7
9 7 2 3 0 1 6 0 0
0 1 8 0 6 0 4 3 0


 


The input matrix is:
  0  0  6  5  0  0  0  0  0
  8  0  9  1  0  4  0  7  0
  0  0  3  0  0  0  2  4  0
  0  8  7  0  0  3  0  6  5
  0  9  0  7  0  0  0  1  0
  4  0  1  0  0  9  7  8  0
  0  6  0  0  8  0  1  0  7
  9  7  2  3  0  1  6  0  0
  0  1  8  0  6  0  4  3  0


 


The answer is:
  1  4  6  5  7  2  8  9  3
  8  2  9  1  3  4  5  7  6
  7  5  3  8  9  6  2  4  1
  2  8  7  4  1  3  9  6  5
  6  9  5  7  2  8  3  1  4
  4  3  1  6  5  9  7  8  2
  3  6  4  9  8  5  1  2  7
  9  7  2  3  4  1  6  5  8
  5  1  8  2  6  7  4  3  9


Press any key to continue . . .


 

No comments:

Post a Comment