📄 sudoko.c
字号:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<getkey.h>
void sudo(int a[9][9])
{
int x,s,b,c,d;
int i,j,i1,j1,rem=0;
for(i1=0;i1<9;i1++)
for(j1=0;j1<9;j1++)
if(a[i1][j1]==0)
rem++;
while(rem!=0)
{ int chk;
int m,n,count=0;
chk=rem;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
int pos[10]={0};
if(a[i][j]==0)
{
for(m=0;m<9;m++)
{
if(a[m][j]!=0&&m!=i)
pos[a[m][j]]=1;
if(a[i][m]!=0&&m!=j)
pos[a[i][m]]=1;
}
}
if(i<3)
s=0;
else if(i>2&&i<6)
s=3;
else if(i>5&&i<9)
s=6;
if(j<3)
b=0;
else if(j>2&&j<6)
b=3;
else if(j>5&&j<9)
b=6;
for(c=s;c<s+3;c++)
for(d=b;d<b+3;d++)
{
if(a[c][d]!=0 &&(c!=i||d!=j))
pos[a[c][d]]=1;
}
count=0;
for(m=1;m<10;m++)
if(!pos[m])
{
count++;
x=m;
}
if(count==1)
{
a[i][j]=x;
rem--;
}
}
if(chk==rem)
break;
}
}
void box(int x,int y,int l,int col,int i)
{
int left=x-l/2;
int top=y-l/2;
int right=x+l/2;
int bottom=y+l/2;
setcolor(col);
rectangle(left,top,right,bottom);
setfillstyle(SOLID_FILL,col);
floodfill(x,y,col);
setcolor(BLACK);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
switch(i)
{
case 1: outtextxy(x-3,y-3,"1");
break;
case 2: outtextxy(x-3,y-3,"2");
break;
case 3: outtextxy(x-3,y-3,"3");
break;
case 4: outtextxy(x-3,y-3,"4");
break;
case 5: outtextxy(x-3,y-3,"5");
break;
case 6: outtextxy(x-3,y-3,"6");
break;
case 7: outtextxy(x-3,y-3,"7");
break;
case 8: outtextxy(x-3,y-3,"8");
break;
case 9: outtextxy(x-3,y-3,"9");
break;
default:outtextxy(x-3,y-3," ");
}
}
void border()
{
int mx = getmaxx();
int my = getmaxy();
setcolor(YELLOW);
rectangle(2,2,mx-2,my-2);
setcolor(RED);
rectangle(4,4,mx-4,my-4);
setcolor(GREEN);
rectangle(6,6,mx-6,my-6);
}
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int arr[9][9];
int i,j,l,m,ch,a,b,c,d ;
/* initialize graphics, local variables
*/
initgraph(&gdriver, &gmode, "c:\\turboc3\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
/* terminate with an error code */
}
ch=2;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
arr[i][j]=0;
a=160;
b=100;
c=195;
d=70;
setcolor(YELLOW);
outtextxy(a,b,"->");
outtextxy(c,d,"|");
outtextxy(c,d+6,"v");
setcolor(RED);
outtextxy(70,7,"USE ARROW KEYS TO POSITION AND ENTER THE NUMBERS IN QUESTION.");
outtextxy(100,20,"DO NOT USE THE NUM-PAD FOR ENTERING NUMBERS.");
outtextxy(90,33,"PRESS SPACE BAR WHEN FINISHED TO DISPLAY ANSWER.");
while(ch!=57)
{
m=100;
for(i=0;i<9;i++)
{ l=195;
for(j=0;j<9;j++)
{
if((i<3||i>5)&&(j<3||j>5)||(i>2&&i<6&&j>2&&j<6))
box(l,m,25,9,arr[i][j]);
else
box(l,m,25,8,arr[i][j]);
l=l+26;
}
m=m+26;
}
ch=getkey();
if(ch==75&&c>195)
{
setcolor(BLACK);
outtextxy(c,d,"|");
outtextxy(c,d+6,"v");
c=c-26;
setcolor(YELLOW);
outtextxy(c,d,"|");
outtextxy(c,d+6,"v");
}
if(ch==80&&b<(100+8*26))
{
setcolor(BLACK);
outtextxy(a,b,"->");
b=b+26;
setcolor(YELLOW);
outtextxy(a,b,"->");
}
if(ch==72&&b>100)
{
setcolor(BLACK);
outtextxy(a,b,"->");
b=b-26;
setcolor(YELLOW);
outtextxy(a,b,"->");
}
if(ch==77&&c<(195+8*26))
{
setcolor(BLACK);
outtextxy(c,d,"|");
outtextxy(c,d+6,"v");
c=c+26;
setcolor(YELLOW);
outtextxy(c,d,"|");
outtextxy(c,d+6,"v");
}
if(ch>1&&ch<11)
{
arr[(b-100)%25][(c-195)%25]=ch-1;
}
}
/*arr[0][1]=2;
arr[0][2]=4;
arr[0][6]=3;
arr[0][8]=6;
arr[1][0]=7;
arr[1][3]=4;
arr[1][5]=6;
arr[1][8]=2;
arr[2][3]=7;
arr[2][6]=8;
arr[3][0]=6;
arr[3][1]=7;
arr[3][2]=8;
arr[3][6]=5;
arr[4][4]=5;
arr[5][2]=5;
arr[5][6]=6;
arr[5][7]=8;
arr[5][8]=1;
arr[6][2]=6;
arr[6][5]=4;
arr[7][0]=8;
arr[7][3]=3;
arr[7][5]=9;
arr[7][8]=5;
arr[8][0]=1;
arr[8][2]=3;
arr[8][6]=9;
arr[8][7]=4; */
m=100;
for(i=0;i<9;i++)
{ l=195;
for(j=0;j<9;j++)
{
if((i<3||i>5)&&(j<3||j>5)||(i>2&&i<6&&j>2&&j<6))
box(l,m,25,9,0);
else
box(l,m,25,8,0);
l=l+26;
}
m=m+26;
}
sudo(arr);
m=100;
for(i=0;i<9;i++)
{ l=195;
for(j=0;j<9;j++)
{
if((i<3||i>5)&&(j<3||j>5)||(i>2&&i<6&&j>2&&j<6))
box(l,m,25,9,arr[i][j]);
else
box(l,m,25,8,arr[i][j]);
l=l+26;
}
m=m+26;
}
getch();
cleardevice();
border();
setcolor(13);
settextstyle(3,0,3);
outtextxy(210,40,"DEVELOPED BY :");
setcolor(9);
settextstyle(4,0,6);
outtextxy(40,180,"LAXMAN D CHOUHAN");
setcolor(12);
settextstyle(1,0,1);
outtextxy(60,330,"PLEASE SEND YOUR SUGGESTIONS AND FEEDBACK AT");
setcolor(14);
settextstyle(3,0,2);
outtextxy(195,348,"laxman_vcet@rediffmail.com");
getch();
closegraph();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -