-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn-squeen.cpp
43 lines (40 loc) · 835 Bytes
/
n-squeen.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
#include<iostream>
#include<cmath>
using namespace std;
#define MaxSize 100
void generateP(int index);
int P[MaxSize];
int hashtable[MaxSize] = {false};
int n = 8;
int count = 0;
int main()
{
// cin >> n;
generateP(1);
cout << count << endl;
return 0;
}
void generateP(int index)
{
if(index == n + 1) {
count++;
return;
}
for(int i = 1; i <= n; i++) {
if(!hashtable[i]) {
hashtable[i] = true;
P[index] = i;
bool flag = true;
for(int k = 1; k < index; k++) {
if(abs(k - index) == abs(i - P[k])) {
flag = false;
break;
}
}
if(flag) {
generateP(index + 1);
}
hashtable[i] = false;
}
}
}