-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path07_n_queen_advance_backtracking.cpp
81 lines (64 loc) · 1.66 KB
/
07_n_queen_advance_backtracking.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
/*
Agenda - Clear some of the backtracking concepts
Questions
---------
N-Queens Problem:
- print/count 1 config
- print/count all config
Approaches
---------
1. Naive Backtracking Approach
2. Backtracing + Bitset (efficient)
3. Backtracking + Bitmasks (more efficient) [Advance Backtracking]
(i.e how to use bitmask during backtracking)
*/
#include <iostream>
using namespace std;
int n;
int count = 0, DONE;
// rowmask denote which positions(columns) in all row are filled
// ld, rd denotes unsafe positions along diagnols for the current row
// DONE is vector of all 11111 (n times 1)
// safe denotes the cols which are safe in current rowss
// More optimisized n queen code !
void solveNQueens(int rowMask, int ld, int rd)
{
// Base Case
if(rowMask == DONE)
{
count++;
return;
}
// Recursive Case
int safe = DONE & (~(rowMask | ld | rd));
while(safe)
{
int p = safe & (-safe);
safe = safe -p;
solveNQueens(rowMask | p, (ld | p) << 1, (rd | p) >> 1);
}
}
// function to drive code
int main()
{
int n;
cout << "Enter Number of Queens: ";
cin >> n;
DONE = ((1 << n) - 1);
solveNQueens(0, 0, 0); // using Bitmask Approach
cout << "Total number of ways to place N-Queens: " << count;
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter Number of Queens: 8
Total number of ways to place N-Queens: 92
Case 2:
Enter Number of Queens: 3
Total number of ways to place N-Queens: 0
Case 3:
Enter Number of Queens: 1
Total number of ways to place N-Queens: 1
*/