-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN_queens.cpp
More file actions
48 lines (42 loc) · 805 Bytes
/
N_queens.cpp
File metadata and controls
48 lines (42 loc) · 805 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
43
44
45
46
47
48
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int a[12] = {0}, b[12] = {0}, i, sum;
bool isafe(int x);
void find(int x);
int main()
{
int N;
for(i = 1; i <= 10; i++){
memset(a, 0, sizeof(a));
sum = 0;
find(0);
b[i] = sum;
}
while((scanf("%d", &N)) != EOF && N){
printf("%d\n",b[N]);
}
return 0;
}
void find(int x){
if(x == i){
sum++;
}else{
for(int j = 0; j < i; j++){
a[x] = j;
if(isafe(x) == false){
continue;
}
find(x + 1);
}
}
}
bool isafe(int x){
for(int i = 0; i < x; i++){
if(a[x] == a[i] || abs(x - i) == abs(a[x] - a[i])){
return false;
}
}
return true;
}