Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*PROGRAM FOR SOLVING THE SUDOKU USING 'C'*/
  2. #include<stdio .h>
  3. #include<conio .h>
  4. #include<iostream .h>
  5. #include<stdlib .h>
  6.  
  7. int i,j,k,a[10][10],o,x[100],y[100];
  8.  
  9. void display();
  10. int getnum();
  11. void solve(int [],int [],int);
  12. int check(int ,int );
  13.  
  14. void main()
  15. {
  16.     clrscr();
  17.     printf("\n\nEnter the elements of SUDOKU in rowwise.\n[ Enter '0' if element is absent. ]");
  18.     for(i=1;i&lt;=9;i++)
  19.     for(j=1;j&lt;=9;j++)
  20.     scanf("%d",&amp;a[i][j]);
  21.     printf("\n\nEntered SUDOKU\n\n");
  22.     display();
  23.     printf("\nEnter any key for solution....\n");
  24.     getch();
  25.     o=getnum();
  26.     solve(x,y,1);
  27. }
  28.  
  29. int getnum()
  30. {
  31.     int c=0;
  32.     for(i=1;i&lt;=9;i++)
  33.     {
  34.         for(j=1;j&lt;=9;j++)
  35.         {
  36.             if(a[i][j]==0)
  37.             {
  38.  
  39.                 x[c]=i;
  40.                 y[c]=j;
  41.             }
  42.         }
  43.     }
  44.     return(c);
  45. }
  46.  
  47. void display()
  48. {
  49.     for(i=1;i&lt;=9;i++)
  50.     {
  51.         for(j=1;j&lt;=9;j++)
  52.         {
  53.             if(a[i][j]!=0)
  54.             printf(" %d",a[i][j]);
  55.             else
  56.             printf(" ");
  57.         }
  58.         printf("\n\n");
  59.     }
  60. }
  61.  
  62. void solve(int p[100],int q[100],int n)
  63. {
  64.     for(k=1;k&lt;=9;k++)
  65.     for(i=p[n];i&lt;=p[n];i++)
  66.     for(j=q[n];j&lt;=q[n];j++)
  67.     {
  68.         a[i][j]=k;
  69.         if(n&lt;0)
  70.         solve(p,q,n++);
  71.         int ch=check(1,0);
  72.         if(ch!=0)
  73.         {
  74.             display();
  75.             getch();
  76.             exit(0);
  77.         }
  78.     }
  79. }
  80.  
  81.  
  82. int check(int n,int r)
  83. {
  84.         int f=0,cont=0;
  85.         if(r==1)
  86.         {
  87.                 for(k=1;k&lt;=9;k++)
  88.                     {
  89.                     for(i=n;i&lt;=n;i++)
  90.                     for(j=1;j&lt;=9;j++)
  91.                     {
  92.                         if(k==a[i][j])
  93.                         f++;
  94.                     }
  95.                 if(f!=1)
  96.                     return(0);
  97.                 else
  98.                     cont++;
  99.                 f=0;
  100.                 }
  101.             if(cont!=9)
  102.                 return(0);
  103.             else if(n==9)
  104.                 check(1,0);
  105.             else
  106.                 check(n++,1);
  107.         }
  108.         else
  109.         {
  110.             for(k=1;k&lt;=9;k++)
  111.             {
  112.                 for(i=1;i&lt;=9;i++)
  113.                 for(j=n;j&lt;=n;j++)
  114.                 {
  115.                 if(k==a[i][j])
  116.                 f++;
  117.                 }
  118.             if(f!=1)
  119.                 return(0);
  120.             else
  121.                 cont++;
  122.             f=0;
  123.             }
  124.         if(cont!=9)
  125.             return(0);
  126.         else if(n!=9)
  127.             check(n++,1);
  128.         }
  129. }