Displaying Source Code(s)
|
|
Graphics Eight Queen (Mini Project)
--------------------------------------------------------------------------------
Description : Solving the eight queens problem using graphics
which helps the beginner to know well about graphics
Code :
//8 QUEEN PROBLEM USING BACKTRACKING
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int col[9],o,f=0,C1=DETECT,C2;
gra();
place(int x);
void main()
{
int row,q;
clrscr();
col[1]=0;
row=1;
printf("
TOTAL NO OF ATTEMPT 92 ");
printf("
NO OF ATTEMPT 'U' WANT : ");
scanf("%d",&o);
while(row>0)
{
col[row]=col[row]+1;
while(col[row]<=8&&place(row)==1)
col[row]=col[row]+1;
if(col[row]<=8)
{
if(row==8)
{
f++;
gra();
if(f==o)
break;
}
else
{
row=row+1;
col[row]=0;
}
}
else
row=row-1;
}
getch();
}
/* PLACING THE QUEEN */
place(int x)
{
int i,p,q,r;
for(i=1;i<x;i++)
{
p=abs(col[i]-col[x]);
q=abs(i-x);
if(col[i]==col[x]||p==q)
return(1);
}
return(2);
}
gra()
{
int s,x,i,c,d,y,j,b,t1,t2,t3,t4;
initgraph(&C1,&C2,"f:");
x=getmaxx();
y=getmaxy();
cleardevice();
x=(x-20)/8;
y=(y-20)/8;
line(10,10,10,(y*8)+10);
line((x*8)+10,10,(x*8)+10,(y*8)+10);
line(10,10,(x*8)+10,10);
line(10,(y*8)+10,(x*8)+10,(y*8)+10);
for(i=1;i<y;i++)
{
line(x+10,10+i,(x*2)+10,10+i);
line((x*5)+10,10+i,(x*6)+10,10+i);
line((x*3)+10,10+i,(x*4)+10,10+i);
line((x*7)+10,10+i,(x*8)+10,10+i);
line(10,y+i+10,x+10,y+i+10);
line((x*2)+10,y+i+10,(x*3)+10,y+i+10);
line((x*4)+10,y+i+10,(x*5)+10,y+i+10);
line((x*6)+10,y+i+10,(x*7)+10,y+i+10);
line(x+10,(y*2)+10+i,(x*2)+10,(y*2)+10+i);
line((x*3)+10,y*2+i+10,(x*4)+10,y*2+i+10);
line((x*5)+10,y*2+i+10,(x*6)+10,y*2+i+10);
line((x*7)+10,y*2+i+10,(x*8)+10,y*2+i+10);
line(10,(y*3)+10+i,x+10,(y*3)+10+i);
line((x*2)+10,y*3+i+10,(x*3)+10,y*3+i+10);
line((x*4)+10,y*3+i+10,(x*5)+10,y*3+i+10);
line((x*6)+10,y*3+i+10,(x*7)+10,y*3+i+10);
line(x+10,(y*4)+10+i,(x*2)+10,(y*4)+10+i);
line((x*3)+10,y*4+i+10,(x*4)+10,y*4+i+10);
line((x*5)+10,y*4+i+10,(x*6)+10,y*4+i+10);
line((x*7)+10,y*4+i+10,(x*8)+10,y*4+i+10);
line(10,(y*5)+10+i,x+10,(y*5)+10+i);
line((x*2)+10,y*5+i+10,(x*3)+10,y*5+i+10);
line((x*4)+10,y*5+i+10,(x*5)+10,y*5+i+10);
line((x*6)+10,y*5+i+10,(x*7)+10,y*5+i+10);
line(x+10,(y*6)+10+i,(x*2)+10,(y*6)+10+i);
line((x*3)+10,y*6+i+10,(x*4)+10,y*6+i+10);
line((x*5)+10,y*6+i+10,(x*6)+10,y*6+i+10);
line((x*7)+10,y*6+i+10,(x*8)+10,y*6+i+10);
line(10,(y*7)+10+i,x+10,(y*7)+10+i);
line((x*2)+10,y*7+i+10,(x*3)+10,y*7+i+10);
line((x*4)+10,y*7+i+10,(x*5)+10,y*7+i+10);
line((x*6)+10,y*7+i+10,(x*7)+10,y*7+i+10);
delay(30);
}
setcolor(2);
c=(x/2)+10;
d=(y/2)+10;
for(i=1,j=0;i<9;i++)
{
s=col[i];
for(b=1;b<11;b++)
circle((c+x*s)-x,d+j,b);
j+=y;
delay(600);
}
delay(3000);
x=getmaxx();
y=getmaxy();
setcolor(16);
for(i=x/2,j=10;i>=0;i--)
{
line(i,10,i,y);
line(x-i,10,x-i,y);
delay(20);
}
t1=10;
t2=8;
t4=1;
i=1;
if(f==o)
{
for(t3=0;t3<=40;t3++)
{
if(t4==10)
{
i++;
t4=1;
}
t4++;
if(t3>=0&&t3<=30)
{
setcolor(i);
outtextxy(t1*t3+8,t2*t3,"H");
outtextxy(620-(t3*9),t2*t3,"Y");
outtextxy(324,t2*t3,"N");
outtextxy(t1*t3+16,480-(t3*8),"A");
outtextxy(615-(t3*8+16),480-(t3*8),"O");
outtextxy(332,480-(t3*8),"K");
outtextxy(t1*t3,240,"T");
outtextxy(608-(t3*8),240,"U");
}
if(t3>=10&&t3<=39)
{
setcolor(16);
outtextxy(t1*(t3-10)+8,t2*(t3-10),"H");
outtextxy(620-(t3-10)*9,t2*(t3-10),"Y");
outtextxy(324,t2*(t3-10),"N");
outtextxy(t1*(t3-10)+16,480-(t3-10)*8,"A");
outtextxy(615-((t3-10)*8+16),480-(t3-10)*8,"O");
outtextxy(332,480-(t3-10)*8,"K");
outtextxy(t1*(t3-10),240,"T");
outtextxy(608-(t3-10)*8,240,"U");
}
delay(50);
}
}
return(0);
}
|
|
|