-
Notifications
You must be signed in to change notification settings - Fork 3
/
queens.c
104 lines (83 loc) · 1.84 KB
/
queens.c
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <time.h>
#include <math.h>
#define SIZE 14
#define ITERATIONS 10
int pos[SIZE];
int used[SIZE];
int down(int line)
{
int j, k, delta, b;
long r, s = 0;
int next;
if (line == SIZE) {
r = 1;
} else {
next = line + 1;
for (j = 0; j < SIZE; j++) {
if (used[j] == 0) {
b = 0;
for (k = 0; k < line; k++) {
delta = line - k;
if (pos[k] + delta == j || pos[k] - delta == j) {
b = 1;
break;
}
}
if (b == 0) {
pos[line] = j;
used[j] = 1;
s += down(next);
used[j] = 0;
}
}
}
r = s;
}
return r;
}
long iteration()
{
int i, half;
long x = 0;
for (i = 0; i < SIZE; i++) {
used[i] = 0;
pos[i] = 0;
}
half = SIZE / 2;
for (i = 0; i < half; i++) {
pos[0] = i;
used[i] = 1;
x += down(1);
used[i] = 0;
}
x = x * 2;
if (SIZE % 2 == 1) {
pos[0] = half;
used[half] = 1;
x += down(1);
}
return x;
}
int main()
{
int i;
long x;
double start, end, dev = 0.0, avg = 0.0, res[ITERATIONS];
for (i = 0; i < ITERATIONS; i++) {
start = clock();
x = iteration();
end = clock();
res[i] = (end - start) / CLOCKS_PER_SEC;
avg += res[i];
}
avg /= ITERATIONS;
for (i = 0; i < ITERATIONS; i++) {
dev += (avg - res[i]) * (avg - res[i]);
}
dev /= (ITERATIONS - 1);
// 68-95-99.7 rule
dev = 3.0 * sqrt(dev);
printf("size:\t%d\nresult:\t%ld\ntime:\t%.4lf +/- %.4lf\n", SIZE, x, avg, dev);
return 0;
}