-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_version.cpp
217 lines (172 loc) · 3.93 KB
/
fast_version.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*@@人工智慧作業-2
@@使用爬山演算法實作八皇后*/
#include <iostream>
#include <random>
#include <vector>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
class NQueen {
protected:
std::vector<std::vector<int>> map;
int _size, top, * X, * Y;
std::default_random_engine generator; //偽亂數產生器
std::random_device rd; //利用硬體系統給的值來得到一個隨機數
std::uniform_int_distribution<int> distribution;
public:
NQueen(int _size) {
this->_size = _size;
this->top = 0;
this->X = new int[this->_size];
this->Y = new int[this->_size];
generator.seed(rd()); //設定偽亂數產生器的seed
//設定亂數分布範圍
std::uniform_int_distribution<int>::param_type param(0, this->_size - 1);
distribution.param(param);
}
~NQueen() {
delete[] this->X;
delete[] this->Y;
}
void set_map() {
map.clear();
//初始化棋盤
for (int i = 0; i < this->_size; i++) {
std::vector<int> tempv;
for (int j = 0; j < this->_size; j++) {
tempv.push_back(0);
}
map.push_back(tempv);
}
for (int i = 0; i < this->top; i++) {
map[X[i]][Y[i]] = 1;
}
}
void print() {
set_map();
for (int i = 0; i < _size; i++) {
for (int j = 0; j < _size; j++) {
if (map[i][j] == 1)
std::cout << "╳ ";
else
std::cout << "ˍ";
}
std::cout << "\n";
}
std::cout << "\n";
}
//heuristic function
int price(int *x, int *y) {
int count = 0;
for (int i = 0; i < this->top - 1; i++) {
double _x = static_cast<double>(x[i]) - static_cast<double>(x[this->top - 1]);
double _y = static_cast<double>(y[i]) - static_cast<double>(y[this->top - 1]);
double m = _y / _x;
if (_x == 0.0 || _y == 0.0 || m == 1.0 || m == -1.0) {
count++;
continue;
}
}
return count;
}
int price_check(int* x, int* y) {
int count = 0;
for (int i = 0; i < _size; i++) {
for (int j = i + 1; j < _size; j++) {
double _x = static_cast<double>(x[i]) - static_cast<double>(x[j]);
double _y = static_cast<double>(y[i]) - static_cast<double>(y[j]);
double m = _y / _x;
if (_x == 0.0 || _y == 0.0 || m == 1.0 || m == -1.0) {
count++;
continue;
}
}
}
return count;
}
void rand_push() {
push(this->distribution(this->generator));
}
void push(int index) {
this->X[this->top] = top;
this->Y[this->top] = index;
this->top++;
}
void pop() {
this->top--;
}
void shuffle(int *data) {
int size = (this->distribution(this->generator) + 1) * this->_size;
for (int i = 0; i < size; i++)
{
int l = this->distribution(this->generator);
int r = this->distribution(this->generator);
std::swap(data[l], data[r]);
}
}
void clear() {
this->top = 0;
}
bool next() {
int* temp = new int[this->_size];
for (int i = 0; i < this->_size; i++)
temp[i] = i;
shuffle(temp);
for (int i = 0; i < this->_size; i++) {
push(temp[i]);
if (price(this->X, this->Y) == 0)
{
delete[] temp;
return true;
}
else
pop();
}
return false;
}
void start() {
int restart = 0;
while (true) {
bool flag = true;
while (true) {
if (this->top == this->_size / 4)
break;
rand_push();
if (price(this->X, this->Y) != 0)
pop();
}
while (true) {
flag = next();
if (!flag || this->top == this->_size)
break;
}
if (flag) {
print();
std::cout << price_check(this->X, this->Y) << std::endl;
break;
}
else {
clear();
restart++;
}
}
std::cout << "restart:" << restart << std::endl;
}
};
int main() {
int n;
while (true) {
std::cin >> n;
if (n <= 3)
break;
LARGE_INTEGER start_time, end_time, frequency_time;
QueryPerformanceFrequency(&frequency_time);
QueryPerformanceCounter(&start_time);
NQueen queen(n);
queen.start();
QueryPerformanceCounter(&end_time);
printf("used:%lf s\n\n", (end_time.QuadPart - start_time.QuadPart) / (double)(frequency_time.QuadPart));
}
system("pause");
return 0;
}