?? sudoku.c
字號:
# include <stdio.h>
# include <conio.h>
int fill(int i,int j,int k);
// this is a sample sudoku; u can use it to test the programme
int box[9][9]={4,0,3,0,0,0,1,0,8,0,8,2,0,0,0,3,7,0,0,0,0,3,0,9,0,0,0,0,0,9,1,0,6,4,0,0,0,2,0,0,0,0,0,9,0,0,0,7,4,0,8,2,0,0,0,0,0,7,0,3,0,0,0,0,3,1,0,0,0,7,2,0,7,0,8,0,0,0,6,0,3}
,counter=1;
void print()
{
int i,j;
printf("\n\n the box is :\n\n");
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf(" %d",box[i][j]);
}
printf("\n\n");
}
}
void process1()
{
int i,j,k,a;
printf("\nprocess one exiquted\n");
counter=0;
for (i=0,k=0;i<9;i++,k++)
{
for(j=0;j<9;j++,k++)
{
if(box[i][j]==0)
{
a=fill(i,j,k);
box[i][j]=a;
if(a!=0)
counter=1;
}
}
k--;
//print();
// getch();
}
}
int fill(int i,int j,int k)
{
int a,b,d,e,r1,c1,s1,hold,r[9],c[9],s[9],uni[9]={0,0,0,0,0,0,0,0,0,},ans[9],all[9]={1,2,3,4,5,6,7,8,9};
//creating row matrix
for(a=0;a<9;a++)
{
r[a]=box[i][a];
}
//creating colem matrix
for(a=0;a<9;a++)
{
c[a]=box[a][j];
}
//creating sub matrix
if(k==0||k==1||k==2||k==9||k==10||k==11||k==18||k==19||k==20)
{
for(a=0,d=0;a<3;a++)
for(b=0;b<3;b++,d++)
s[d]=box[a][b];
}
if(k==3||k==4||k==5||k==12||k==13||k==14||k==21||k==22||k==23)
{
for(a=0,d=0;a<3;a++)
for(b=3;b<6;b++,d++)
s[d]=box[a][b];
}
if(k==6||k==7||k==8||k==15||k==16||k==17||k==24||k==25||k==26)
{
for(a=0,d=0;a<3;a++)
for(b=6;b<9;b++,d++)
s[d]=box[a][b];
}
if(k==27||k==28||k==29||k==36||k==37||k==38||k==45||k==46||k==47)
{
for(a=3,d=0;a<6;a++)
for(b=0;b<3;b++,d++)
s[d]=box[a][b];
}
if(k==30||k==31||k==32||k==39||k==40||k==41||k==48||k==49||k==50)
{
for(a=3,d=0;a<6;a++)
for(b=3;b<6;b++,d++)
s[d]=box[a][b];
}
if(k==33||k==34||k==35||k==42||k==43||k==44||k==51||k==52||k==53)
{
for(a=3,d=0;a<6;a++)
for(b=6;b<9;b++,d++)
s[d]=box[a][b];
}
if(k==54||k==55||k==56||k==63||k==64||k==65||k==72||k==73||k==74)
{
for(a=6,d=0;a<9;a++)
for(b=0;b<3;b++,d++)
s[d]=box[a][b];
}
if(k==57||k==58||k==59||k==66||k==67||k==68||k==75||k==76||k==77)
{
for(a=6,d=0;a<9;a++)
for(b=3;b<6;b++,d++)
s[d]=box[a][b];
}
if(k==60||k==61||k==62||k==69||k==70||k==71||k==78||k==79||k==80)
{
for(a=6,d=0;a<9;a++)
for(b=6;b<9;b++,d++)
s[d]=box[a][b];
}
//performing union
for(a=0;a<9;a++)
{
r1=r[a];
c1=c[a];
s1=s[a];
if(r1!=0)
uni[r1-1]=r1;
if(c1!=0)
uni[c1-1]=c1;
if(s1!=0)
uni[s1-1]=s1;
}
//performing complement
for(a=0;a<9;a++)
{
ans[a]=all[a]-uni[a];
}
//checking for no of elements in ans matrix
for(a=0,b=0;a<9;a++)
{
if(*(ans+a)!=0)
{
hold=a;
b++;
}
}
// returning the value
if(b==1)
{
a=*(ans+hold);
return a;
}
else
return 0;
}
int main()
{
int i,j;
printf("#####################################################\n");
printf(" this programme will solve easy sudoku \n");
printf(" AUTHOR : P SANDEEP REDDY ");
printf("\n#####################################################\n");
// print();
printf(" /nenter suduko numbers in order if there is on no enter zero :\n");
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
scanf("%d",&box[i][j]);
}
while(counter==1)
process1();
printf( "\n\n\n\nthe ans is\n\n");
print();
getch();
return ;
}
// this programme cannot solve all sudoku's only easy once it can solve
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -