0% found this document useful (0 votes)
50 views5 pages

Experiment7 AA

The document discusses implementing the 8 queens problem in C/C++. It provides an algorithm and source code to place 8 queens on a chessboard such that no two queens attack each other and print all possible solutions. The code uses backtracking to try all possible placements of queens from left to right to find all configurations satisfying the constraints.

Uploaded by

Tanishka Mayekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

Experiment7 AA

The document discusses implementing the 8 queens problem in C/C++. It provides an algorithm and source code to place 8 queens on a chessboard such that no two queens attack each other and print all possible solutions. The code uses backtracking to try all possible placements of queens from left to right to find all configurations satisfying the constraints.

Uploaded by

Tanishka Mayekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Implement 8

queen’s problem
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science

Experiment No. 7

AIM: Implement 8 queen’s problem

Required Software/ Software Tool


- Windows Operating System
-C/C++

Theory:

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. For example, following is the output matrix for above 4 queen solution.

{ 0, 1, 0, 0}
{ 0, 0, 0, 1}
{ 1, 0, 0, 0}
{ 0, 0, 1, 0}

Generate all possible configurations of queens on board and print a configuration


that satisfies the given constraints.

Algorithm:
1) Start in the leftmost column
2) If all queens are placed return true
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 queen in [row, column] leads to a solution then return true.
c. If placing queen doesn't lead to a solution then unmark this [row, column]
(Backtrack) and go to step (a) to try other rows.
4) If all rows have been tried and nothing worked, return false to trigger Backtracking.
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science

Implementing the Solution Writing Source Code:


//Tanishka Mayekar printf("Enter the number of Queens\n");
#include<stdlib.h> scanf("%d",&n);
#include<stdio.h> queen(n);
#include<math.h> printf("\nTotal solutions=%d",count);
int a[30],count=0; }
int place(int pos){
int i;
for(i=1;i<pos;i++){
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-
pos))))
return 0;
}
return 1;
}
void print_sol(int n){
int i,j; count++;
printf("\n\nSolution #%d:\n",count);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(a[i]==j)

printf("Q\t");
else
printf("*\t");
}
printf("\n");
}
}
void queen(int n){
int k=1;
a[k]=0;
while(k!=0){
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n){
if(k==n)
print_sol(n);
else{
k++;
a[k]=0;
}
}
else
k--;
}
}
void main(){
int i,n;
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science

Input and Output


Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science

Conclusion:
In this experiment we have implemented 8 queen’s problem.

You might also like