-
Notifications
You must be signed in to change notification settings - Fork 0
/
heuristic-n-queens.cpp
executable file
·126 lines (117 loc) · 3.07 KB
/
heuristic-n-queens.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
#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
pair<vector<int>,int> data;
int n;
double T;
void init() {
cout << "Input n = ";
cin >> n;
T = n;
srand(time(0));
vector<int> a;
for(int i = 0; i < n; i++) {
a.push_back(i);
//data.first.push_back(i);
}
for(int i = 0; i < n; i++) {
int temp = random() % a.size();
data.first.push_back(a[temp]);
a.erase(a.begin() + temp);
}
int result = 0;
for(int i = 0 ; i < n - 1; i++) {
for(int j = i + 1 ; j < n; j++){
if(j - i == data.first[j] - data.first[i] || data.first[i] + i == data.first[j] + j){
result++;
}
}
}
data.second = result;
}
pair<vector<int>,int> Next_move(pair<vector<int>,int> data, int i, int j) {
pair<vector<int>,int> temp = data;
int heuristic = data.second;
int jj = data.first[i];
int ii = data.first[j];
temp.first[i] = ii;
temp.first[j] = jj;
int count1 = 0, count2 = 0;
for (int k = 0; k < data.first.size(); k++) {
if (k != i) {
if (ii + i == temp.first[k] + k
|| ii - i == temp.first[k] - k) {
count1++;
}
if (jj + i == data.first[k] + k
|| jj - i == data.first[k] - k) {
count2++;
}
}
if (k != j) {
if (jj + j == temp.first[k] + k
|| jj - j == temp.first[k] - k) {
count1++;
}
if (ii + j == data.first[k] + k
|| ii - j == data.first[k] - k) {
count2++;
}
}
}
temp.second = (heuristic - count2 + count1);
return temp;
}
void show(pair<vector<int>,int> data) {
for(int i = 0; i < n; i++) {
cout << data.first[i] << " ";
}
}
void solve() {
srand(time(0));
double p;
int step = 0;
while(T > 0) {
if(data.second == 0){
show(data);
break;
}
T *= 0.99;
int i = rand()%n;
int j = rand()%n;
pair<vector<int>, int> next_move = Next_move(data,i,j);
if(next_move.second == 0) {
cout << next_move.second << endl;
cout << "Steps: " << step << endl;
cout << "Result: ";
show(next_move);
break;
}
int delta = next_move.second - data.second;
if(delta < 0) {
data = next_move;
step++;
cout << next_move.second << endl;
}
else {
p = exp(-delta/double(T));
if(rand()/double(RAND_MAX) < p){
data = next_move;
step++;
cout << next_move.second << endl;
}
}
}
}
int main() {
clock_t start = clock();
init();
cout << "The first value of heuristic: " << data.second << endl;
solve();
clock_t finish = clock();
cout << endl << "Time running: " << finish - start << " micro seconds" << endl;
return 0;
}