Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

C Code for Sudoku

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

int i,j,k,a[10][10],o,x[100],y[100];

void display();
int getnum();
void solve(int [],int [],int);
int check(int ,int );

void main()
{
clrscr();
printf("\n\nEnter the elements of <strong class="highlight">SUDOKU</strong> in rowwise.\n[ Enter '0' if element is absent. ]");
<strong class="highlight">for</strong>(i=1;i<=9;i++)
<strong class="highlight">for</strong>(j=1;j<=9;j++)
scanf("%d",&a[i][j]);
printf("\n\nEntered SUDOKU\n\n");
display();
printf("\nEnter any key <strong class="highlight">for</strong> solution....\n");
getch();
o=getnum();
solve(x,y,1);
}

int getnum()
{
int c=0;
<strong class="highlight">for</strong>(i=1;i<=9;i++)
{
<strong class="highlight">for</strong>(j=1;j<=9;j++)
{
if(a[i][j]==0)
{
<strong class="highlight">c++</strong>;
x[c]=i;
y[c]=j;
}
}
}
return(c);
}

void display()
{
<strong class="highlight">for</strong>(i=1;i<=9;i++)
{
<strong class="highlight">for</strong>(j=1;j<=9;j++)
{
if(a[i][j]!=0)
printf(" %d",a[i][j]);
else
printf(" ");
}
printf("\n\n");
}
}

void solve(int p[100],int q[100],int n)
{
<strong class="highlight">for</strong>(k=1;k<=9;k++)
<strong class="highlight">for</strong>(i=p[n];i<=p[n];i++)
<strong class="highlight">for</strong>(j=q[n];j<=q[n];j++)
{
a[i][j]=k;
if(n<0)
solve(p,q,n++);
int ch=check(1,0);
if(ch!=0)
{
display();
getch();
exit(0);
}
}
}
}

int check(int n,int r)
{
int f=0,cont=0;
if(r==1)
{
<strong class="highlight">for</strong>(k=1;k<=9;k++)
{
<strong class="highlight">for</strong>(i=n;i<=n;i++)
<strong class="highlight">for</strong>(j=1;j<=9;j++)
{
if(k==a[i][j])
f++;
}
if(f!=1)
return(0);
else
cont++;
f=0;
}
if(cont!=9)
return(0);
else if(n==9)
check(1,0);
else
check(n++,1);
}
else
{
<strong class="highlight">for</strong>(k=1;k<=9;k++)
{
<strong class="highlight">for</strong>(i=1;i<=9;i++)
<strong class="highlight">for</strong>(j=n;j<=n;j++)
{
if(k==a[i][j])
f++;
}
if(f!=1)
return(0);
else
cont++;
f=0;
}
if(cont!=9)
return(0);
else if(n!=9)
check(n++,1);
}
}

 

 

 
 
Job or extra money for students

Search Engine Rank of your blog or websites