-
Notifications
You must be signed in to change notification settings - Fork 1
/
NQueen.cpp
63 lines (53 loc) · 1.03 KB
/
NQueen.cpp
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
56
57
58
59
60
61
62
63
#include <bits\stdc++.h>
int a[50],counter = 0;
using namespace std;
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;
counter ++;
cout<<"\n\nSoluton #"<<counter<<endl;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(a[i]==j){
cout<<"Q\t";
}
else{
cout<<"*\t";
}
}
cout<<"\n";
}
}
void queen(int n){
int k = 1;
a[k] = 0;
while (k!=0){
do{
a[k]++;
}while ((a[k]<=n) &&! place(k));
if(a[k]<=n){
if(k==n)
print_sol(n);
else{
k++;
a[k]=0;
}
}
else
k--;
}
}
int main(){
int n;
cout<<"Enter the number of Queen's :\t";
cin>>n;
queen(n);
cout<<"\nTotal solution "<< counter;
}