Professional Documents
Culture Documents
INTRODUCTION
In backtracking, we first take a step and then we see if this step taken is correct or not i.e.,
whether it will give a correct answer or not. And if it doesn’t, then we just come back and change
our first step. In general, this is accomplished by recursion. Thus, in backtracking, we first start
with a partial sub-solution of the problem (which may or may not lead us to the solution) and
then check if we can proceed further with this sub-solution or not. If not, then we just come back
and change it.
1
1.1 ABOUT THE CASE STUDY
In backtracking, we first take a step and then we see if this step taken is correct or not i.e.,
whether it will give a correct answer or not. And if it doesn’t, then we just come back and change
our first step. In general, this is accomplished by recursion. Thus, in backtracking, we first start
with a partial sub-solution of the problem (which may or may not lead us to the solution) and
then check if we can proceed further with this sub-solution or not. If not, then we just come back
2
If not, then come back and change the sub-solution and continue again
One of the most common examples of the backtracking is to arrange N queens on an NxN
chessboard such that no queen can strike down any other queen. A queen can attack horizontally,
vertically, or diagonally. The solution to this problem is also attempted in a similar way. We first
place the first queen anywhere arbitrarily and then place the next queen in any of the safe places.
We continue this process until the number of unplaced queens becomes zero (a solution is found)
or no safe place is left. If no safe place is left, then we change the position of the previously
placed queen.
Backtracking Algorithm
The idea is to place queens one by one in different columns, starting from the leftmost column.
When we place a queen in a column, we check for clashes with already placed queens. In the
current column, if we find a row for which there is no clash, we mark this row and column as
part of the solution. If we do not find such a row due to clashes then we backtrack and return
false.
return true
3
3) Try all rows in the current column. Do following for every tried row.
a) If the queen can be placed safely in this row then mark this [row,
column] as part of the solution and recursively check if placing queen here leads to a solution.
true.
3) If all rows have been tried and nothing worked, return false to trigger
backtracking.
4
2. HARDWARE AND SOFTWARE SPECIFICATION
HARDWARE SPECIFICATIONS
The selection of hardware configuration is very important task related to the software
development. Insufficient RAM may affect adversely on the speed and efficiency of the entire
system. The processor should be powerful to handle the entire operations. The hard disk should
have sufficient capacity to store the file applications.
RAM : 2 GB
Mouse : 3 Buttons
SOFTWARE SPECIFICATIONS
The software is desired for DOS version 5 or later. It can also work in Windows 95 or later
version.
OS : Windows 7
5
Back end : MS DOS
6
3. SCREEN LAYOUT
7
8
9
10
4. CODING
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
int diag45[12],sol[12],n,chess[12][12],r1,c1,num=0,i,x1,x2,y2,y1;
int num1,num2,m=0,x3;
void main()
int gd=DETECT,gm,x,y;
void queens(int,int);
clrscr();
cin>>n;
initgraph(&gd,&gm,"c:\tc\bgi");
x=getmaxx();
y=getmaxy();
setcolor(WHITE);
x1=y1=0;
11
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;
//y1=y1+y2;
//x2=x2+x/n;
//y2=y2+y/n;
x1=x1-x2;
x3=x1;
12
for(i=0;i<=n;i++)
delay(250);
line(x1,y1,0,y1);
//x1=x1+x2;
if(i==1)
num2=y1/2;
y1=y1+y2;
//x2=x2+x/n;
//y2=y2+y/n;
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);
13
x1=x1+10;
for(i=0;i<=n;i++)
delay(250);
line(x1,y1,0,y1);
y1=y1+10;
x1=5;
y1=5;
delay(250);
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;
14
//sol[1]=1;
//int c1=0;
//while(c1!=7)
//{
num1=x1;
queens(0,c1);
//c1++;
//}
setcolor(WHITE);
getch();
delay(10);
int d45,d135,atck=0;
if(r>(n-1)||c>(n-1))
if(m==1)
15
setcolor(BLACK);
setfillstyle(1,BLACK);
fillellipse(x1+((c-1)*x2),y1+(r*y2),7,7);
m=0;
setcolor(WHITE);
setfillstyle(1,WHITE);
fillellipse(x1+((c)*x2),y1+(r*y2),6,6);
delay(350);
//getch();
/*delay(100);
*/
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))//This condition
else //attack
16
{
atck=1;
break;
}}
if(atck!=1)
//setcolor(WHITE);
//pieslice(x1+(c*num2*2),y1+(r*num1*2),0,360,4);
sol[r]=c;
diag45[r]=r-c;
if(r==(n-1))
//for(i=0;i<n;i++)
//cout<<””;
num+
getch();
if(c==(n-1))
//sol[r-1]=sol[r-1]+1;
17
setcolor(BLACK);
setfillstyle(1,BLACK);
fillellipse(x1+((n-1)*x2),y1+((n-1)*y2),7,7);
m=1;
//queens(r-1,sol[r-1]);
else
m=1;
queens(r,c+1);
}}
//*********//
else
/*for(i=0;i<r;i++)
printf("%d ",sol[i]);*/
sol[r+1]=0;
queens(r+1,0);
if(c!=(n-1))
queens(r,c+1);
else
18
{
setcolor(BLACK);
setfillstyle(1,BLACK);
fillellipse(x1+((c)*x2),y1+(r*y2),6,6);
cout<<"";
}}}
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(100);
19
setbkcolor(BLACK);
cout<<"";
}}
else
if(c!=(n-1))
cout<<"";
// else
}}
20
5. CONCLUSION
The THE N QUEENS PROBLEM aims at the implementation of the use of Backtracking in
C++. Proper system study has been conducted and findings in critical area have been
incorporated. The case study has successfully completed and tested and implemented after
giving necessary instructions to the operator. This case study is well documented and
validated.
Successful implementation of the system requires a lot of effort from customers and
developers side. Lack of awareness of computer and how the system operates will affect the
performance of the system. Proper system study is also required for the successful
completion of the Case study. The system design though it has met the user’s needs has its
own limitations.
21
6. BIBLIOGRAPHY & WEBLIOGRAPHY
G.L. Heileman, Data Structures, Algorithms and Object Oriented Programming, 1st edition.
Horowitz, E and Sahni, Sartaj, Fundamentals of Data Structures, 1st edition, – Galgotia
www.scanftree.com/data_structures
www.tutorialspoint.com/data_structures_algorithms
www.studytonight.com/data_structures
22