-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12. 8-QUEENS.CPP
More file actions
55 lines (50 loc) · 732 Bytes
/
12. 8-QUEENS.CPP
File metadata and controls
55 lines (50 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#define R 50
using namespace std;
int x[R], count;
int place(int k,int i)
{
int j;
for(j=1;j<k;j++)
{
if( x[j]==i || abs(x[j]-i)==abs(j-k) )
return 0;
}
return 1;
}
void NQueens(int k,int n)
{
int i,j;
for(i=1;i<=n;i++)
{
if(place(k,i)==1)
{
x[k]=i;
if(k==n)
{
count++;
cout<<"Solution "<<count<<".\n";
cout<<" Row:Column = ";
for(j=1;j<=n;j++)
cout<<j<<":"<<x[j]<<" ";
cout<<endl;
}
else NQueens(k+1,n);
}
}
}
int main()
{
int n;
cout<<" *** 8-Queens Chess Problem ***\n";
do
{ cout<<"\nEnter number of Queens(0 to quit) : ";
cin>>n;
count=0;
NQueens(1,n);
}while(n);
return 0;
}