-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnqueen_best_soln.cpp
More file actions
42 lines (39 loc) · 831 Bytes
/
nqueen_best_soln.cpp
File metadata and controls
42 lines (39 loc) · 831 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
#include <iostream>
using namespace std;
int board[100][100] = {0};
int n,ans=0,DONE;
int returnPosition(int n){
int ans = 0;
while(n>0){
ans++;
n = n>>1;
}
return ans-1;
}
void solve(int rowMask,int ld,int rd,int row){
if(rowMask==DONE){
ans++;
for(int i = 0;i<n;++i){
for(int j = 0;j<n;++j)
cout<<board[i][j];
cout<<"\n";
}
cout<<"\n";
return;
}
int safe = DONE & (~(rowMask|ld|rd));
while(safe){
int p = safe & (-safe);
int col = returnPosition(p);
board[row][col] = 1;
safe = safe - p;
solve(rowMask|p,(ld|p)<<1,(rd|p)>>1,row+1);
board[row][col] = 0;
}
}
int main() {
cin>>n;
DONE = ((1<<n))-1;
solve(0,0,0,0);
cout<<ans;
}