-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucs.cpp
61 lines (59 loc) · 1.34 KB
/
ucs.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
#include "sokoban.h"
struct UCSState {
int cost;
State state;
UCSState() {}
UCSState(int c) : cost(c) {}
bool operator < (const UCSState &b) const
{
return cost > b.cost;
}
};
/**
* Uniform Cost Search
*/
void UCS(const vector<string> &ground)
{
int time1 = clock();
Statistics stat;
priority_queue<UCSState>q;
StateSet rec;
UCSState init(0);
initState(ground, init.state);
vector<State>stateVector;
q.push(init);
stateVector.push_back(init.state);
rec.insert(init.state);
UCSState result;
while (!q.empty()) {
UCSState tmp = q.top();
q.pop();
for (int i = 0; i < 4; ++i) {
UCSState now = tmp;
now.state.person.x += direction[i][0];
now.state.person.y += direction[i][1];
int s = validState(direction[i][0], direction[i][1], now.state, ground);
now.state.move = step[i];
now.state.previousStateNum = now.state.currentStateNum;
stat.anodes++;
if (s == -1) {
result = now;
goto end;
} else if (s && !rec.count(now.state)) {
now.cost += s;
now.state.currentStateNum = stateVector.size();
stateVector.push_back(now.state);
rec.insert(now.state);
q.push(now);
} else if (s) {
stat.bnodes++;
}
}
}
end:
stat.cnodes = q.size();
stat.dnodes = rec.size() + 1;
stat.runtime = (clock() - time1) * 1.0 / CLOCKS_PER_SEC;
outputStat(stat);
outputSolution(stateVector, result.state);
}