-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31_n_queens_b_boxes_permute.cpp
66 lines (57 loc) · 1.36 KB
/
31_n_queens_b_boxes_permute.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
// n -queens
// b - boxes
// permute them
// so for 2 queens and 4 boxes -
// ans-
// 4p2 i.e. 12
// q1b0q2b1
// q1b0q2b2
// q1b0q2b3
// q1b1q2b0
// q1b1q2b2
// q1b1q2b3
// q1b2q2b0
// q1b2q2b1
// q1b2q2b3
// q1b3q2b0
// q1b3q2b1
// q1b3q2b2
#include<bits/stdc++.h>
using namespace std;
void print_permutations(int cq, int tq, int tb, int* visited, string asf)
{
if(cq>=tq)
{
if(cq==tq)
cout<<asf<<endl;
return;
}
for(int i=0; i<tb; i++)
{
if(visited[i]==0)
{
visited[i] = 1;
print_permutations(cq+1, tq, tb, visited, asf + "q" + to_string(cq) + " b" + to_string(i) + " ");
visited[i] = 0;
}
// WE CAN AVOID USING AN ARRAY TO KEEP VISITED INFORMATION
// INSTEAD USE BITMASKING AS ILLUSTRATED -
// boxes is an integer, initialized as 0
// for (int i=0; i<tb ;i++)
// {
// if((boxes & (1<<i)) ==0) // check if ith place is 1
// {
// boxes = boxes ^ (1<<i); // change ith bit to 1
// printperm(cq+1, boxes, tq, tb, asf + "q"+to_string(cq)+"b"+to_string(i));
// boxes = boxes ^ (1<<i);
// }
// }
}
}
int main()
{
int tq = 2;
int tb = 4;
int visited[] = {0,0,0,0};
print_permutations(0, 2, 4, visited, "");
}