-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathChapter11_SearchAlgorithms.cpp
More file actions
144 lines (126 loc) · 3.12 KB
/
Chapter11_SearchAlgorithms.cpp
File metadata and controls
144 lines (126 loc) · 3.12 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
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
#include <stdio.h>
#include <Windows.h>
#include <math.h>
#include <queue>
#include <set>
#include <memory>
#include "WindowCode.h"
#define TILE_COST 1
class AStarNode;
typedef std::shared_ptr<AStarNode> AStarNodePtr;
class AStarNode
{
public:
int x, y;
int g, score;
AStarNodePtr parent;
AStarNode(int x, int y, int cost, AStarNodePtr p, int score = 0)
: x(x), y(y), g(cost), score(score), parent(p)
{}
static AStarNodePtr makePtr(int x, int y, int cost, AStarNodePtr p, int score = 0)
{
return AStarNodePtr(new AStarNode(x, y, cost, p, score));
}
int heuristic(const int destx, int desty) const
{
int xd = destx - x;
int yd = desty - y;
return abs(xd) + abs(yd);
}
void updateScore(int endx, int endy)
{
auto h = this->heuristic(endx, endy) * TILE_COST;
this->score = g + h;
}
AStarNodePtr getCopy()
{
return AStarNode::makePtr(x, y, g, parent, score);
}
std::vector<AStarNodePtr> getChildren(int width, int height)
{
std::vector<AStarNodePtr> ret;
auto copy = getCopy();
if (x > 0)
ret.push_back(AStarNode::makePtr(x - 1, y, g + TILE_COST, copy));
if (y > 0)
ret.push_back(AStarNode::makePtr(x, y - 1, g + TILE_COST, copy));
if (x < width - 1)
ret.push_back(AStarNode::makePtr(x + 1, y, g + TILE_COST, copy));
if (y < height - 1)
ret.push_back(AStarNode::makePtr(x, y + 1, g + TILE_COST, copy));
return ret;
}
};
bool operator<(const AStarNodePtr &a, const AStarNodePtr &b)
{
return a->score > b->score;
}
bool operator==(const AStarNodePtr &a, const AStarNodePtr &b)
{
return a->x == b->x && a->y == b->y;
}
template<int WIDTH, int HEIGHT>
void makeList(AStarNodePtr end, std::vector<AStarNodePtr> nodes, int path[WIDTH][HEIGHT])
{
for (auto n = nodes.begin(); n != nodes.end(); n++)
path[(*n)->x][(*n)->y] = 2;
AStarNodePtr node = end;
while (node.get() != nullptr)
{
path[node->x][node->y] = 1;
node = node->parent;
}
}
template<int WIDTH, int HEIGHT, int BLOCKING>
bool doAStarSearch(
int map[WIDTH][HEIGHT],
int startx, int starty,
int endx, int endy,
int path[WIDTH][HEIGHT])
{
std::priority_queue<AStarNodePtr> frontier;
std::vector<AStarNodePtr> allNodes;
auto node = AStarNode::makePtr(startx, starty, 0, nullptr);
node->updateScore(endx, endy);
allNodes.push_back(node);
while (true)
{
if (node->x == endx && node->y == endy)
{
makeList<WIDTH, HEIGHT>(node, allNodes, path);
return true;
}
auto children = node->getChildren(WIDTH, HEIGHT);
for (auto c = children.begin(); c != children.end(); c++)
{
if (map[(*c)->x][(*c)->y] == BLOCKING)
continue;
auto found = std::find(allNodes.rbegin(), allNodes.rend(), *c);
if (found != allNodes.rend())
{
if (*found > *c)
{
(*found)->g = (*c)->g;
(*found)->parent = (*c)->parent;
(*found)->updateScore(endx, endy);
}
}
else
{
(*c)->updateScore(endx, endy);
frontier.push(*c);
allNodes.push_back(*c);
}
}
if (frontier.size() == 0)
return false;
node = frontier.top();
frontier.pop();
}
}
int main (int argc, char *argv[])
{
searchFunction = doAStarSearch<XSIZE, YSIZE, BLOCKING_TILE>;
showWindow();
return 0;
}