a*寻路算法c++
FindWay.h
1 #ifndef __FINDWAY_H__ 2 #define __FINDWAY_H__ 3 4 #include "global.h" 5 #include <iostream> 6 #include <list> 7 #include <numeric> 8 #include <algorithm> 9 10 using namespace std; 11 12 class LLK_Vector 13 { 14 public: 15 int x; 16 int y; 17 LLK_Vector(); 18 LLK_Vector(int _x, int _y); 19 bool operator==(LLK_Vector& v1); 20 }; 21 22 class LLK_Point 23 { 24 public: 25 int id; 26 LLK_Vector position; 27 int h; 28 int g; 29 LLK_Point *father; 30 31 LLK_Point(); 32 LLK_Point(LLK_Vector _position, int _h, int _g, LLK_Point* F); 33 bool operator==(LLK_Point& v1); 34 }; 35 36 class WayFinding 37 { 38 public: 39 ~WayFinding(); 40 WayFinding(); 41 list<LLK_Point*> *Open_List; 42 list<LLK_Point*> *Close_List; 43 LLK_Point* FindWay(int birds[ROW][COL], LLK_Point* pa, LLK_Point* pb); 44 private: 45 LLK_Point* GetMinF(); 46 bool IsBar(LLK_Point* p, int map[ROW][COL]); 47 bool IsInCloseList(LLK_Vector* p); 48 LLK_Point* GetPointFromCloseList(LLK_Vector* p); 49 bool IsInOpenList( LLK_Vector* p ); 50 LLK_Point* GetPointFromOpenList(LLK_Vector* p); 51 int GetG( LLK_Point* p ); 52 int GetH(LLK_Vector* p, LLK_Vector* cp); 53 void CheckPoint(LLK_Point* p0, int map[ROW][COL], LLK_Point* pa, LLK_Point& pb); 54 55 56 }; 57 58 //delete object 59 template<class T> 60 extern void Delete(T* v); 61 #endif //__FINDWAY_H__
FindWay.cpp
1 #include "FindWay.h" 2 3 //delete object 4 template<class T> 5 void Delete(T* v) 6 { 7 if (v != NULL) 8 { 9 delete v; 10 v = NULL; 11 } 12 } 13 14 void remove_list(list<LLK_Point*>& l, LLK_Point& n) 15 { 16 list<LLK_Point*>::iterator i = l.begin(); 17 while(i != l.end()) 18 { 19 if (**i == n) 20 { 21 l.erase(i); 22 break; 23 } 24 i++; 25 } 26 } 27 28 LLK_Vector::LLK_Vector() 29 { 30 31 } 32 33 LLK_Vector::LLK_Vector(int _x, int _y) 34 { 35 this->x = _x; 36 this->y = _y; 37 } 38 39 bool LLK_Vector::operator==(LLK_Vector& v1) 40 { 41 if (this->x == v1.x && this->y == v1.y) 42 { 43 return true; 44 } 45 else 46 { 47 return false; 48 } 49 } 50 51 LLK_Point::LLK_Point() 52 { 53 this->g = 0; 54 this->h = 0; 55 this->father = NULL; 56 } 57 LLK_Point::LLK_Point(LLK_Vector _position, int _h, int _g, LLK_Point *F) 58 { 59 this->position = _position; 60 this->h = _h; 61 this->g = _g; 62 this->father = F; 63 } 64 65 bool LLK_Point::operator == (LLK_Point& v1) 66 { 67 if (this->g == v1.g && this->h == v1.h && this->position == v1.position) 68 { 69 return true; 70 } 71 return false; 72 } 73 74 WayFinding::~WayFinding() 75 { 76 while(Open_List->size() > 0) 77 { 78 LLK_Point* tempPoint = Open_List->front(); 79 Delete(tempPoint); 80 Open_List->pop_front(); 81 } 82 83 while(Close_List->size() > 0) 84 { 85 LLK_Point* tempPoint = Close_List->front(); 86 Delete(tempPoint); 87 Close_List->pop_front(); 88 } 89 90 Delete(Open_List); 91 Delete(Close_List); 92 } 93 94 WayFinding::WayFinding() 95 { 96 Open_List = new list<LLK_Point*>(); 97 Close_List = new list<LLK_Point*>(); 98 } 99 100 LLK_Point* WayFinding::GetMinF() 101 { 102 LLK_Point* _pMin = NULL; 103 104 for (list<LLK_Point*>::iterator p = this->Open_List->begin(); p != this->Open_List->end(); p++) 105 { 106 if ( _pMin == NULL || _pMin->g + _pMin->h > (*p)->g + (*p)->h ) 107 { 108 if (_pMin == NULL) 109 { 110 _pMin = new LLK_Point(); 111 *_pMin = **p; 112 break; 113 } 114 115 } 116 } 117 return _pMin; 118 119 } 120 121 bool WayFinding::IsBar(LLK_Point* p, int map[ROW][COL]) 122 { 123 int x = p->position.x; 124 int y = p->position.y; 125 if ( map[x, y] == 0 ) 126 { 127 return true; 128 } 129 return false; 130 131 } 132 bool WayFinding::IsInCloseList(LLK_Vector* p) 133 { 134 for ( list<LLK_Point*>::iterator _p = Close_List->begin(); _p != this->Close_List->end(); _p++ ) 135 { 136 if ( p->x == (*_p)->position.x && p->y == (*_p)->position.y ) 137 { 138 return true; 139 } 140 } 141 return false; 142 143 } 144 LLK_Point* WayFinding::GetPointFromCloseList(LLK_Vector* p) 145 { 146 for ( list<LLK_Point*>::iterator _p = Close_List->begin(); _p != this->Close_List->end(); _p++ ) 147 { 148 if ( p->x == (*_p)->position.x && p->y == (*_p)->position.y ) 149 { 150 LLK_Point* temp = new LLK_Point(); 151 *temp = **_p; 152 return temp; 153 } 154 } 155 return NULL; 156 157 } 158 159 bool WayFinding::IsInOpenList( LLK_Vector* p ) 160 { 161 for (list<LLK_Point*>::iterator _p = this->Open_List->begin(); _p != this->Open_List->end(); _p++) 162 { 163 if ( p->x == (*_p)->position.x && p->y == (*_p)->position.y ) 164 { 165 return true; 166 } 167 } 168 return false; 169 } 170 171 172 LLK_Point* WayFinding::GetPointFromOpenList(LLK_Vector* p) 173 { 174 for (list<LLK_Point*>::iterator _p = this->Open_List->begin(); _p != this->Open_List->end(); _p++) 175 { 176 if ( p->x == (*_p)->position.x && p->y == (*_p)->position.y ) 177 { 178 LLK_Point* temp = new LLK_Point(); 179 *temp = **_p; 180 return temp; 181 } 182 } 183 return NULL; 184 185 } 186 187 int WayFinding::GetG( LLK_Point* p ) 188 { 189 if ( p->father == NULL ) 190 return 0; 191 if ( p->position.x == p->father->position.x || p->position.y == p->father->position.y ) 192 { 193 return ( p->father->g + 10 ); 194 } 195 else 196 { 197 return ( p->father->g + 14 ); 198 } 199 } 200 201 202 int WayFinding::GetH(LLK_Vector* p, LLK_Vector* cp) 203 { 204 return (abs( p->x - cp->x ) + abs( p->y -cp->y ) )*10; 205 206 } 207 208 void WayFinding::CheckPoint(LLK_Point* p0, int map[ROW][COL], LLK_Point* pa, LLK_Point& pb) 209 { 210 int x = 0, y = 0; 211 for ( int i = 0; i < 4; i++ ) 212 { 213 switch(i) 214 { 215 case 0: 216 x = p0->position.x + 1; 217 y = p0->position.y; 218 break; 219 case 1: 220 x = p0->position.x - 1; 221 y = p0->position.y; 222 break; 223 case 2: 224 x = p0->position.x; 225 y = p0->position.y - 1; 226 break; 227 case 3: 228 x = p0->position.x; 229 y = p0->position.y + 1; 230 break; 231 } 232 if ( x >= 0 && x < COL && y >= 0 && y < ROW ) 233 { 234 LLK_Vector* tempVec = new LLK_Vector( x , y ); 235 if ( map[y][x] == 0 && !IsInCloseList( tempVec ) ) 236 { 237 if ( IsInOpenList( tempVec ) ) 238 { 239 LLK_Point *pt = GetPointFromOpenList( tempVec ); 240 int G_new = 0; 241 242 G_new = GetG( pt ); 243 if ( G_new < pt->g ) 244 { 245 //Open_List->remove( *pt ); 246 remove_list(*Open_List, *pt); 247 pt->father = p0; 248 pt->g = G_new; 249 Open_List->push_back ( pt ); 250 } 251 } 252 else 253 { 254 LLK_Point *pt = new LLK_Point (); 255 pt->position.x = x; 256 pt->position.y = y; 257 pt->father = p0; 258 pt->g = GetG( pt ); 259 pt->h = GetH( &pt->position, &pb.position ); 260 Open_List->push_back( pt ); 261 } 262 } 263 264 Delete(tempVec); 265 } 266 } 267 268 } 269 270 LLK_Point* WayFinding::FindWay(int birds[ROW][COL], LLK_Point* pa, LLK_Point* pb) 271 { 272 LLK_Point* head = new LLK_Point (); 273 Open_List->push_back(pa); 274 275 while ( !( IsInOpenList( &pb->position ) || Open_List->size() == 0 ) ) 276 { 277 LLK_Point* p0 = GetMinF(); 278 if ( p0 == NULL ) 279 { 280 break; 281 } 282 //Open_List->remove( *p0 ); 283 remove_list(*Open_List, *p0); 284 Close_List->push_back( p0 ); 285 CheckPoint( p0, birds, pa, *pb ); 286 } 287 LLK_Point *p = GetPointFromOpenList( &pb->position ); 288 289 LLK_Point* tempP = new LLK_Point (); 290 if ( p != NULL ) 291 { 292 tempP->position.x = p->position.x; 293 tempP->position.y = p->position.y; 294 tempP->father = NULL; 295 296 head = tempP; 297 p = p->father; 298 } 299 else 300 { 301 return NULL; 302 } 303 304 305 while ( p->father != NULL ) 306 { 307 LLK_Point* temp = new LLK_Point (); 308 309 temp->position.x = p->position.x; 310 temp->position.y = p->position.y; 311 temp->father = head; 312 313 head = temp; 314 p = p->father; 315 } 316 return head; 317 }

浙公网安备 33010602011771号