-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathN-queen.cpp
70 lines (60 loc) · 987 Bytes
/
N-queen.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int m = 1e3;
int n;
bool p[m+1][m+1];
bool _isPlace(int r,int c){
for (int i = 1; i < c; ++i){
if(p[r][i])
return false;
}
int i,j;
for (i = r, j = c; i > 0 && j > 0; i--, j--){
if(p[i][j])
return false;
}
for (i = r, j = c; i <= n && j > 0; i++, j--){
if(p[i][j])
return false;
}
return true;
}
bool _N_Queens(int c){
if(c > n )
return true;
for (int i = 1; i <= n; ++i){
if(_isPlace(i, c)){
p[i][c] = true;
if(_N_Queens(c+1))
return true;
p[i][c] = false; // Back tracking
}
}
return false;
}
int main(){
cin>>n;
memset(p,false,sizeof(p));
if(n==1){
cout<<"YES\n1"<<endl;
}
else if(n<4){
cout<<"NO"<<endl;
}
else{
bool ans = _N_Queens(1);
if(ans){
cout<<"YES"<<endl;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
cout<<p[i][j]<<" ";
}
cout<<endl;
}
}
else
cout<<"NO"<<endl;
}
return 0;
}