-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmain.cpp
More file actions
142 lines (136 loc) · 4.12 KB
/
Copy pathmain.cpp
File metadata and controls
142 lines (136 loc) · 4.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
//
// main.cpp
//
// A*寻路算法
//
// Created by vincent on 2017/10/17.
// Copyright © 2017年 vincent. All rights reserved.
//
#include <iostream>
#include <list>
// 节点结构
struct Node {
Node(int _x, int _y):x(_x), y(_y){};
int x; // 地图坐标x
int y; // 地图坐标y
int g; // 距离起点的距离
int h; // 距离终点的距离
int f; // g + h
Node *p = nullptr; // 父节点
};
// 地图,0表示通路,1表示障碍,5表示路径
const int Size = 10;
int Map[Size][Size] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0,
5, 0, 1, 1, 0, 1, 1, 5, 1, 0,
0, 1, 1, 0, 0, 1, 1, 0, 1, 0,
0, 1, 0, 0, 1, 1, 0, 0, 1, 0,
0, 1, 0, 1, 1, 0, 0, 1, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
};
// 开放列表
std::list<Node *> openList;
// 关闭列表
std::list<Node *> closeList;
// 寻路算法
bool Astar(Node *start, Node *end) {
openList.push_back(start);
while (!openList.empty()) {
// 选出f值最小的节点
auto min = openList.begin();
for (auto iter = openList.begin(); iter != openList.end(); ++iter) {
if ((*iter)->f < (*min)->f) {
min = iter;
}
}
Node *node = *min;
openList.erase(min);
closeList.push_back(node);
// 处理周围的四个点
int surrounds[4][2] = {
node->x, node->y - 1, // 上
node->x, node->y + 1, // 下
node->x - 1, node->y, // 左
node->x + 1, node->y, // 右
};
for (int i = 0; i < 4; ++i) {
int x = surrounds[i][0];
int y = surrounds[i][1];
// 跳过超出边界的点
if (x < 0 || x >= Size || y < 0 || y >= Size) {
continue;
}
// 跳过不可通行的点
if (Map[x][y] == 1) {
continue;
}
// 跳过在关闭列表里的点
bool isInCloseList = false;
for (auto iter = closeList.begin(); iter != closeList.end(); ++iter) {
if (x == (*iter)->x && y == (*iter)->y) {
isInCloseList = true;
break;
}
}
if (isInCloseList) {
continue;
}
// 跳过在开放列表里的点
bool isInOpenList = false;
for (auto iter = openList.begin(); iter != openList.end(); ++iter) {
if (x == (*iter)->x && y == (*iter)->y) {
isInOpenList = true;
break;
}
}
if (isInOpenList) {
continue;
}
// 判断是否到达终点
if (x == end->x && y == end->y) {
end->p = node;
return true;
}
// 剩下的加入开放列表
Node *newNode = new Node(x, y);
newNode->g = abs(x - start->x) + abs(y - start->y);
newNode->h = abs(x - end->x) + abs(y - end->y);
newNode->f = newNode->g + newNode->h;
newNode->p = node;
openList.push_back(newNode);
}
}
return false;
}
int main(int argc, const char * argv[]) {
Node *start = new Node(4, 0);
Node *end = new Node(4, 7);
Map[start->x][start->y] = 5;
Map[end->x][end->y] = 5;
for (int i = 0; i < Size; ++i) {
for (int j = 0; j < Size; ++j) {
printf("%d\t", Map[i][j]);
}
printf("\n");
}
printf("-------------------------开始寻路-------------------------\n");
bool isReachEnd = Astar(start, end);
if (isReachEnd) {
Node *node = end;
while (node) {
Map[node->x][node->y] = 5;
node = node->p;
}
}
for (int i = 0; i < Size; ++i) {
for (int j = 0; j < Size; ++j) {
printf("%d\t", Map[i][j]);
}
printf("\n");
}
return 0;
}