-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnqueen.cpp
85 lines (82 loc) · 1.44 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
#define list vector<int>
using namespace std;
class nqueen{
vector<list> sol;
int n;
bool **b;
public:
nqueen(int v){
n=v;
b=(bool**)malloc(n*sizeof(bool*));
for(int i=0;i<n;i++){
bool *f=(bool*)malloc(n*sizeof(bool));
for(int j=0;j<n;j++)
*(f+j)=false;
*(b+i)=f;
}
}
bool issafe(int i,int j){
for(int k=0;k>=0 and k<n;k++){
if(b[k][j])
return false;
}
for(int k=0;k>=0 and k<n;k++){
if(b[i][k])
return false;
}
for(int k=i,l=j;k>=0 and l>=0;k--,l--){
if(b[k][l])
return false;
}
for(int k=i,l=j;k<n and l>=0;k++,l--){
if(b[k][l])
return false;
}
return true;
}
bool rec(int r,int c){
if(c==n){
list v;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(b[i][j]){
v.push_back(j);
}
}
}
sol.push_back(v);
return true;
}
bool f=false;
for(int i=0;i<n;i++){
if(issafe(i,c)){
b[i][c]=1;
f=rec(r,c+1);
b[i][c]=0;
}
}
return f;
}
void soln(){
rec(0,0);
if(sol.size()){
sort(sol.begin(),sol.end());
cout<<"total solution "<<sol.size()<<endl;
for(int i=0;i<sol.size();i++){
for(int j=0;j<sol[i].size();j++)
cout<<sol[i][j]<<" ";
cout<<endl;
}
}
else
cout<<"NO OP";
}
};
int main(void){
int n;
cout<<"Enter number of queens ";
cin>>n;
nqueen q=nqueen(n);
q.soln();
}