Experiment No:-09: Aim: Write A Program To Implement N Queen's Problem Using Back Tracking. Theory
Experiment No:-09: Aim: Write A Program To Implement N Queen's Problem Using Back Tracking. Theory
Experiment No:-09: Aim: Write A Program To Implement N Queen's Problem Using Back Tracking. Theory
Experiment No:-09
Aim: Write a Program to Implement N Queen's problem using Back Tracking.
Theory:
N-Queen problem:
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two
queens attack each other. For example, following is a solution for 4 Queen problem. The
expected output is a binary matrix which has 1s for the blocks where queens are placed.
This problem is to find an arrangement of N queens on a chess board, such that no queen can
attack any other queens on the board. The chess queens can attack in any direction as horizontal,
vertical, horizontal and diagonal way.
The n-queens completion puzzle is a form of mathematical problem common in computer
science and described as “NP-complete”. These are interesting problems because if an efficient
solution can be found for one NP-complete problem, it can be used to solve all NP-complete
problems.
It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2 and
n =3. So first we will consider the 4 queens problem and then generate it to n - queens problem.
Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.
DS_41_Pratik Tikande
Since, we have to place 4 queens such as q1 q2 q3 and q4 on the chessboard, such that no two
queens attack each other. In such a conditional each queen must be placed on a different row,
i.e., we put queen "i" on row "i."
Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put queen q 2 so that
both these queens do not attack each other. We find that if we place q 2 in column 1 and 2, then
the dead end is encountered. Thus the first acceptable position for q 2 in column 3, i.e. (2, 3) but
then no position is left for placing queen 'q 3' safely. So we backtrack one step and place the
queen 'q2' in (2, 4), the next best possible solution.
Then we obtain the position for placing 'q 3' which is (3, 2). But later this position also leads to a
dead end, and no place is found where 'q 4' can be placed safely. Then we have to backtrack till
'q1' and place it to (1, 2) and then all other queens are placed safely by moving q 2 to (2, 4), q3 to
(3, 1) and q4 to (4, 3). That is, we get the solution (2, 4, 1, 3). This is one possible solution for the
4-queens problem. For another possible solution, the whole method is repeated for all partial
solutions. The other solutions for 4 - queens problems is (3, 1, 4, 2) i.e.
The implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows:
DS_41_Pratik Tikande
Fig shows the complete state space for 4 - queens problem. But we can use backtracking method
to generate the necessary node and stop if the next node violates the rule, i.e., if two queens are
attacking.
DS_41_Pratik Tikande
It can be seen that all the solutions to the 4 queens problem can be represented as 4 - tuples (x 1,
x2, x3, x4) where xi represents the column on which queen "qi" is placed.
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.
Algorithm:
Program :
#include<stdio.h>
#include<math.h>
int board[20],count;
DS_41_Pratik Tikande
int main()
{
int n,i,j;
void queen(int row,int n);
printf(" - N Queens Problem Using Backtracking -");
printf("\n\nEnter number of Queens:");
scanf("%d",&n);
queen(1,n);
return 0;
}
//function for printing the solution
void print(int n)
{
int i,j;
printf("\n\nSolution %d:\n\n",++count);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
/*funtion to check conflicts
If no conflict for desired postion returns 1 otherwise returns 0*/
int place(int row,int column)
{
DS_41_Pratik Tikande
int i;
for(i=1;i<=row-1;++i)
{
//checking column and digonal conflicts
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
return 1; //no conflicts
}
//function to check for proper positioning of queen
void queen(int row,int n)
{
int column;
for(column=1;column<=n;++column)
{
if(place(row,column))
{
board[row]=column; //no conflicts so place queen
if(row==n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}
}
DS_41_Pratik Tikande
Output:
4 Queen
8 Queen:
DS_41_Pratik Tikande