BACKTRACKING : The n-Queen Problem
     
Home Page

About Page

Contact Page

Custom Page

 

Backtracking Example : The n-Queen problem


This program is a graphic program that simulates the now classic n-Queen problem of backtracking. It is also commonly known as the 8-queen problem,due the 8X8 size of a normal chess board.

The C-program :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
int diag45[20],sol[20],n,chess[20][20],r1,c1,num=0,i,x1,x2,y2,y1;
int num1,num2,m=0,x,y;
void main()
{
int gd=DETECT,gm;
void queens(int,int);
clrscr();
printf("Enter the size of the chess board (12 is the limit) ");
scanf("%d",&n);
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=getmaxx();
y=getmaxy();
setcolor(WHITE);
x1=y1=0;
x2=x/n;
y2=y/n;
if(x2>=10)
{
if(x2>y2)
x2=y2;
else
y2=x2;
for(i=0;i<=n;i++)
{
delay(250);
line(x1,y1,x1,y);
if(i==1)
num1=x1/2;
x1=x1+x2;
}
x1=x1-x2;
num2=x1;
for(i=0;i<=n;i++)
{
delay(250);
line(x1,y1,0,y1);
y1=y1+y2;
}
x1=(x/n)/2;
y1=(y/n)/2;
} /* if(x2>10)*/
else
{
x2=y2=10;
for(i=0;i<=n;i++)
{
delay(250);
line(x1,y1,x2,y2);
x1=x1+10;
}
for(i=0;i<=n;i++)
{
delay(250);
line(x1,y1,0,y1);
y1=y1+10;
}

x1=5;
y1=5;
}
for(i=0;i<n;i++)
sol[i]=32767;
for(i=0;i<n;i++)
{for(int j=0;j<n;j++)
{chess[i][j]=j;
}}
for(int j=0;j<n;j++)
diag45[j]=sol[j]=32767;
num1=x1;
outtextxy(num2+2,0,"Hit any key to exit ");
queens(0,c1);
setcolor(BLACK);
outtextxy(num2+2,y1,"Hit any key to ");
outtextxy(num2+2,y1+12,"continue ");
setcolor(WHITE);
outtextxy(num2+2,y1,"All solutions are ");
outtextxy(num2+2,y1+12,"over ");
printf("Total solutions are %d",num);
getch();
}
void queens(int r,int c)
{
delay(300);
if(!kbhit())
{
int d45,d135,atck=0;
if(r>(n-1)||c>(n-1))
printf("Nonsensical condition reached ");
if(m==1)
{
setcolor(BLACK);
setfillstyle(1,BLACK);
fillellipse(x1+((c-1)*x2),y1+(r*y2),7,7);
}
m=0;
setcolor(GREEN);
setfillstyle(1,GREEN);
fillellipse(x1+((c)*x2),y1+(r*y2),6,6);
delay(350);
d45=r-c;
for(i=0;i<r;i++)
{
r1=abs(r-i);
c1=abs(c-sol[i]);
if((chess[r][c]!=sol[i])&&(d45!=diag45[i])&&(r1!=c1))
continue;
else
{
atck=1;
break;
}}
if(atck!=1)
{
sol[r]=c;
diag45[r]=r-c;
if(r==(n-1))
{
num++;
setcolor(BLACK);
outtextxy(num2+2,0,"Hit any key to exit ");
setcolor(WHITE);
outtextxy(num2+2,y1,"Hit any key to ");
outtextxy(num2+2,y1+12,"continue ");
getch();
setcolor(BLACK);
outtextxy(num2+2,y1,"Hit any key to ");
outtextxy(num2+2,y1+12,"continue ");
setcolor(WHITE);
outtextxy(num2+2,0,"Hit any key to exit ");
if(c==(n-1))
{
setcolor(BLACK);
setfillstyle(1,BLACK);
fillellipse(x1+((n-1)*x2),y1+((n-1)*y2),7,7);
m=1;
}
else
{
m=1;
queens(r,c+1);
}}
//*********//
else
{
sol[r+1]=0;
queens(r+1,0);
if(c!=(n-1))
queens(r,c+1);
else
{
setcolor(BLACK);
setfillstyle(1,BLACK);
fillellipse(x1+((c)*x2),y1+(r*y2),6,6);
printf("");
}}}
//If an attack is taking place
else
{
setfillstyle(1,BLACK);
setcolor(BLACK);
fillellipse(x1+(c*x2),y1+(r*y2),6,6);
  if(r!=0)
  {
  if(c!=(n-1))
  queens(r,c+1);
  else
  {
  m=1;
  setbkcolor(RED);
  delay(200);
  setbkcolor(BLACK);
  printf("");
  }}

  else
  {
  if(c!=(n-1))
  printf("");
  }

}}}

 


 

How to run the program.

One can run this program on TurboC++ IDE. Select the text of the program,copy it,and paste it in MS notepad or MS Word,or MS Wordpad.Then save it as a cpp file.



This was my project for the subject, Computer Simulation and Modelling.

Go to the custom page to view the explanation of the algorithm.


 


 
   
 

7819