/
pathfind.cpp
391 lines (324 loc) · 8.98 KB
/
pathfind.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stdlib.h>
#include <iostream>
#include "pathfind.h"
bool NodeSort(pNode node1,pNode node2)
{
return (node1->F > node2->F);
}
CPathFind::CPathFind() {
// TODO Auto-generated constructor stub
m_map = NULL;
m_row = 0;
m_col = 0;
//int** path = NULL;
m_startx = 0;
m_starty = 0;
m_endx = 0;
m_endy = 0;
m_step = 0;
}
CPathFind::~CPathFind() {
// TODO Auto-generated destructor stub
}
void CPathFind::SetMap(int row, int col, int** map) {
if (map != NULL) {
m_row = row;
m_col = col;
m_map = map;
}
m_path = new int *[m_row];
for(int i = 0;i < m_row;i++)
{
m_path[i]=new int[m_col];
}
int* a = (int*)&map[0];
int* b = (int*)m_path[0];
for (int i = 0 ;i< m_row ;i++)
{
for(int j= 0 ;j< m_col;j++)
{
b[j]=a[j];
}
a =a+ m_col;
b =b+ m_col;
}
}
int CPathFind::CalcG(int x, int y) {
return (10 * (abs(m_endx - x) + abs(m_endy - y)));
}
pNode CPathFind::CreateFirstNode(int x, int y)
{
pNode node = new Node;
node->x = x;
node->y = y;
node->H = 0;
node->G = CalcG(x, y);
node->F = node->H + node->G;
node->father = NULL;
return node;
}
void CPathFind::InitNode(int startx,int starty,int endx,int endy)
{
m_startnode = new Node();
m_startnode->x = startx;
m_startnode->y = starty ;
m_startnode->G = CalcG(startx,starty);
m_startnode->H = 0 ;
m_startnode->F = m_startnode->G+ m_startnode->H;
m_startnode->father = NULL;
m_endnode = new Node();
m_endnode->x = endx;
m_endnode->y = endy ;
m_endnode->G = CalcG(endx,endy);
m_endnode->H = 0 ;
m_endnode->F = m_endnode->G+ m_endnode->H;
m_endnode->father = NULL;
}
bool CPathFind::CheckIsDestination(pNode node)
{
//判断是否已经搜寻到目标节点
if (node->x == m_endx && node->y == m_endy)
{
cout << "到达目标地点!!!" << endl;
if(m_endnode !=NULL)
{
m_endnode->G = node->G;
m_endnode->H = node->H;
m_endnode->F = node->F;
m_endnode->father = node->father;
return true;
}
else
return false;
}
else
return false;
}
void CPathFind::generatePath()
{
Node *nodeChild = m_endnode;
Node *nodeParent = m_endnode->father;
while((nodeChild->x != m_startnode->x) or (nodeChild->y != m_startnode->y))
{
((int*)m_path[nodeChild->x])[nodeChild->y] = 2;//标记为路径上的点
cout<<"Path:"<<nodeChild->x<<nodeChild->y<<endl;
nodeChild = nodeParent;
if (nodeParent !=NULL)
nodeParent = nodeParent->father;
}
((int*)m_path[m_startnode->x])[m_startnode->y]= 2;//标记为路径上的点
}
bool CPathFind::GetShiftByDirectory(int dir , int * x , int* y)
{
switch (dir) {
case 0:
*x = -1;
*y = 0;
break;
case 1:
*x = -1;
*y = 1;
break;
case 2:
*x= 0;
*y= 1;
break;
case 3:
*x = 1;
*y = 1;
break;
case 4:
*x= 0 ;
*y = 1 ;
break;
case 5:
*x = -1;
*y = 1;
break;
case 6:
*x = 0;
*y = -1;
break;
case 7:
*x = -1;
*y = -1;
break;
default:
x = 0 ;
y = 0 ;
break;
}
return true;
}
//该函数是判断数据是否违法
bool CPathFind::isIllegle(int x, int y)
{
//Printmap();
int * a = (int*)((&m_map[0]));
for (int k = 0 ;k<x ;k++)
{
a = a+m_col;
}
cout<<"---------------begin"<<x<<"----------------"<<endl;
for(int i =0;i<16;i++)
{
cout<<a[i];
}
cout<<""<<endl;
cout<<"---------------end----------------"<<endl;
int value = a[y];
cout<<"isIllegle:"<<x<<y<<endl;
if (x >= 0 && x < m_row &&
y >= 0 && y < m_col &&
value == 1)
return true;
else
{
cout<<"无效点"<<endl;
return false;
}
}
bool CPathFind::PathFind(int startx, int starty, int endx, int endy) {
InitNode(startx,starty,endx,endy);
//首先做几个预先判断
if ((startx == endx) && (starty == endy)) {
cout << "起点就是终点";
}
m_startx = startx;
m_starty = starty;
m_endx = endx;
m_endy = endy;
pNode node = CreateFirstNode(startx, starty);
//将这个节点放入openlist
m_openlist.push_back(node);
//对这个地方进行排序
push_heap(m_openlist.begin(),m_openlist.end(),NodeSort);
pNode tmpnode = NULL;
for(;;)
{
if(m_openlist.empty())
{
cout<<"错误:不能找到目标节点"<<endl;
return false;
}
tmpnode = m_openlist.front();
++m_step;
pop_heap(m_openlist.begin(),m_openlist.end(),NodeSort);
m_openlist.pop_back();
if (!CheckIsDestination(tmpnode))
{
//这里不是是目标地点
for(int i = 0 ;i<8 ;i++)
{
int nextx ;
int nexty ;
GetShiftByDirectory(i,&nextx,&nexty);
nextx = tmpnode->x + nextx;
nexty = tmpnode->y + nexty;
//判断这个点是可以通过的
cout<<"next is"<<nextx<<":"<<nexty<<endl;
if(isIllegle(nextx,nexty))
{
//这里可以通过
//计算这个点的G值
int newGvalue;
if(i % 2 ==0)
newGvalue = tmpnode->G+10;
else
newGvalue = tmpnode->G+14;
vector<pNode>::iterator OpenIt;
//说明该节点在OPEN表中
for(OpenIt=m_openlist.begin();OpenIt<m_openlist.end();OpenIt++)
{
if (((*OpenIt)->x == nextx)&&((*OpenIt)->y ==nexty))
{
break;
}
}
if(OpenIt != m_openlist.end())
{
if ((*OpenIt)->G <= newGvalue)
continue;
}
//说明该节点在close表中
vector<pNode>::iterator CloseIt;
for(CloseIt=m_closelist.begin();CloseIt<m_closelist.end();CloseIt++)
{
if (((*CloseIt)->x == nextx)&&((*CloseIt)->y ==nexty))
{
break;
}
}
if(CloseIt != m_closelist.end())
{
if ((*CloseIt)->G <= newGvalue)
continue;
}
//如果都不满足上边的条件那么说明这个节点是最优节点
Node *bestNode = new Node;
bestNode->x = nextx;
bestNode->y = nexty;
bestNode->father = tmpnode;
bestNode->G = newGvalue;
bestNode->H = CalcG(nextx,nexty);
bestNode->F = bestNode->G + bestNode->H;
if (CloseIt != m_closelist.end())
{
delete(*CloseIt);
m_closelist.erase(CloseIt);
}
if (OpenIt != m_openlist.end())
{
delete(*OpenIt);
m_openlist.erase(OpenIt);
make_heap(m_openlist.begin(),m_openlist.end(),NodeSort);
}
m_openlist.push_back(bestNode);
push_heap(m_openlist.begin(),m_openlist.end(),NodeSort);
for(vector<pNode>::iterator k = m_openlist.begin() ;k<m_openlist.end();k++)
{
cout<<"x:"<<(*k)->x<<",y:"<<(*k)->y<<endl;
}
cout<<" isIlleglea is true"<<endl;
}
else
{
cout<<" isIlleglea is false"<<endl;
//不能通过
}
}
m_closelist.push_back(tmpnode);
}
else
{
generatePath();
return true;
}
}
}
void CPathFind::Printmap()
{
int * a = (int*)(&m_map[0]);
for(int j = 0 ;j<m_row;j++)
{
for(int i= 0 ;i<m_col;i++)
{
cout<<a[i]<<";";
}
cout<<""<<endl;
a = (int*)(&m_map[0])+(j+1)*m_col;
}
}
void CPathFind::PrintPath()
{
int * a = (int*) m_path[0];
for(int i = 0 ;i<m_row;i++)
{
for(int j= 0 ;j<m_col;j++)
{
cout<<a[j]<<";";
}
cout<<""<<endl;
a = a+m_col;
}
}