-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheightqueen.cpp
More file actions
106 lines (88 loc) · 1.88 KB
/
eightqueen.cpp
File metadata and controls
106 lines (88 loc) · 1.88 KB
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
105
106
/*
* eight queee iteration solution.
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <sys/time.h>
#include <cstdio>
using namespace std;
const int boardsize= 8;
void init(vector<int> & s)
{
for (int i = 0; i < s.size(); i++)
s[i] = i;
}
bool check(vector<int> & a)
{
for (int i = 0; i < a.size(); i++) {
for (int j = i+1; j < a.size(); j++) {
if ((a[i] == a[j]) || (abs(a[i]-a[j]) == j-i)) {
return false;
}
}
}
return true;
}
void show(const vector<int> & a)
{
for (int i = 0; i < a.size(); i++)
printf("%d\t", a[i]);
//cout << a[i] << "\t";
printf("\n");
//cout << endl;
}
int putqueen(vector<int> & a)
{
int num = 0;
while (next_permutation(a.begin(), a.end()))
if (check(a)) {
//show(a);
num++;
}
return num;
}
bool check(vector<int> a, int n)
{
for (int i = 0; i < n; i++)
if (a[i] == a[n] || n-i == abs(a[i] - a[n]))
return false;
return true;
}
int putqueen_recur(vector<int> & a, int n = 0)
{
static int solu = 0;
if (n == a.size()) {
//show(a);
solu++;
}
else {
for (int i = 0; i < a.size(); i++) {
a[n] = i;
if (check(a, n))
putqueen_recur(a, n+1);
}
}
return solu;
}
int main(int argc, char *argv[])
{
int re;
struct timeval st, en;
gettimeofday(&st, NULL);
vector<int> board(boardsize);
#if 0
init(board);
re = putqueen(board);
#endif
#if 1
re = putqueen_recur(board);
#endif
cout << "There are totally " << re;
cout << " solutions.\n";
gettimeofday(&en, NULL);
cout << "Total time is " << (en.tv_sec - st.tv_sec) * 1000.0 +
(en.tv_usec - st.tv_usec) / 1000.0 << "ms" << endl;
return 0;
}