You are on page 1of 22

1.

INTRODUCTION

In this we are describing about the application of backtracking known as “N QUEENS


PROBLEM”. It is developed using C++ in DOS platform. C++ is a programming language,
which is based object oriented programming.
Backtracking is finding the solution of a problem whereby the solution depends on the previous
steps taken. For example, in a maze problem, the solution depends on all the steps you take one-
by-one. If any of those steps is wrong, then it will not lead us to the solution. In a maze problem,
we first choose a path and continue moving along it. But once we understand that the particular
path is incorrect, then we just come back and change it. This is what backtracking basically is.

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

Backtracking is an algorithmic-technique for solving problems recursively by trying to build a


solution incrementally, one piece at a time, removing those solutions that fail to satisfy the
constraints of the problem at any point of time (by time, here, is referred to the time elapsed till
reaching any level of the search tree).Generally, every constraint satisfaction problem which has
clear and well-defined constraints on any objective solution, that incrementally builds candidate
to the solution and abandons a candidate (“backtracks”) as soon as it determines that the
candidate cannot possibly be completed to a valid solution, can be solved by Backtracking.
However, most of the problems that are discussed, can be solved using other known algorithms
like Dynamic Programming or Greedy Algorithms in logarithmic, linear, linear-logarithmic time
complexity in order of input size, and therefore, outshine the backtracking algorithm in every
respect (since backtracking algorithms are generally exponential in both time and space).
However, a few problems still remain, that only have backtracking algorithms to solve them until
now.

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.

Thus, the general steps of backtracking are:

start with a sub-solution

check if this sub-solution will lead to the solution or not

2

If not, then come back and change the sub-solution and continue again

N queens on NxN chessboard

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.

1) Start in the leftmost column

2) If all queens are placed

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.

b) If placing the queen in [row, column] leads to a solution then return

true.

c) If placing queen doesn't lead to a solution then umark this [row,

column] (Backtrack) and go to step (a) to try other rows.

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.

Processor : Intel Pentium Processor

RAM : 2 GB

Hard disk capacity : 40 GB

Monitor : SVGA color monitor

Keyboard : 108 keys and above

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.

Front end : C++

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();

cout<<"Enter the size of the chess board(12 is the limit) ";

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);

outtextxy(x3,y1,"All solutions are over ");

cout<<"Total number of solutions is "<<num;

getch();

void queens(int r,int c)

delay(10);

int d45,d135,atck=0;

if(r>(n-1)||c>(n-1))

cout<<"Nonsensical condition reached ";

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);

cout<<”r is”<<r<<”c is”<<c;

*/

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

continue; //checks for an

else //attack

16
{

atck=1;

break;

}}

//If the queen is safe :-

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))

//cout<<” the absolute solution is:”;

//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<<"";

}}}

//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(100);

19
setbkcolor(BLACK);

cout<<"";

}}

else

if(c!=(n-1))

cout<<"";

// else

// cout<<”all solutions are over”;

}}

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

Samanta D, Classic Data Structures, 2nd edition, Prentice Hall of India.

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

You might also like