forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN-Queen.cpp
84 lines (76 loc) · 1.6 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Solution of the famous N-queen problem
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long>vl;
typedef pair<int,int>pi;
typedef vector<vector<int>>vii;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for (int i = a; i < b; i++)
bool safe(int row,int col,vii &board){
rep(i,0,col){
if(board[row][i]==1){
return false;
}
}
for(int i=row,j=col;i>=0 and j>=0;i--,j--){
if(board[i][j]==1)return false;
}
for(int i=row,j=col;i<=board.size() and j>=0;i++,j--){
if(board[i][j]==1)return false;
}
return true;
}
bool solve(int col,int n,vii &board,vii &res){
if(col==n){
vi v;
rep(i,0,n)
rep(j,0,n){
if(board[i][j]){
v.push_back(j+1);
}
}
res.push_back(v);
return true;
}
rep(row,0,n)
if(safe(row,col,board)){
board[row][col]=1;
solve(col+1,n,board,res);
board[row][col]=0;
}
return false;
}
vii nqueen(int n){
vii board(n,vi(n,0));
vii res;
solve(0,n,board,res);
sort(res.begin(),res.end());
return res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<vector<int>>ans;
ans=nqueen(n);
if(ans.size()==0){
cout<<-1<<endl;
}
else{
rep(i,0,ans.size()){
cout<<"[";
for(auto it: ans[i]){
cout<<it<<" ";
}
cout<<"]";
}
cout<<endl;
}
return 0;
}