关于AStar的算法请参见网络.

在些大体介绍一下.

AStar算法的核心是估价函数,不同的估价函数可能会表现出不同的行为,因此效率也会有所不同.是一种启发式搜索.有一张open和close表,使用这两张表来确定哪些遍历过,确定下一个节点.

 

astar.h

 

 
  
  1. /************************ 
  2. ## author:Dante Lee 
  3. ## email:bhlzlx@gmail.com 
  4. ## qq:583035284 
  5. ************************/ 
  6. #ifndef ASTAR_H_INCLUDED 
  7. #define ASTAR_H_INCLUDED 
  8.  
  9. #include <stdio.h> 
  10. #include <stdlib.h> 
  11. #include <assert.h> 
  12.  
  13. #define MAP_INITED  1 
  14. // cell types 
  15. #define WALKABLE    0 
  16. #define UNWALKABLE  1 
  17. #define YES         1 
  18. #define NO          0 
  19.  
  20. // the struct contains all the basic information for a AStar Cell. 
  21. typedef struct AStarCell 
  22.     // the position 
  23.     int x; 
  24.     int y; 
  25.     // attribute 
  26.     short int cellType; 
  27. } *pAStarCell,AStarCell_; 
  28.  
  29. ///contains the enssential information for the Node, 
  30. /// for example 1.parent node 2.the node's cost and so on. 
  31. typedef struct AStarCellNode 
  32.     //AStarCell 
  33.     pAStarCell pCell; 
  34.     //the cost 
  35.     float staticCost; 
  36.     float predictCost; 
  37.     float totalCost; 
  38.     //neighbor cells 
  39.     struct AStarCellNode * lpUp; 
  40.     struct AStarCellNode * lpRightUp; 
  41.     struct AStarCellNode * lpRight; 
  42.     struct AStarCellNode * lpRightDown; 
  43.     struct AStarCellNode * lpDown; 
  44.     struct AStarCellNode * lpLeftDown; 
  45.     struct AStarCellNode * lpLeft; 
  46.     struct AStarCellNode * lpLeftUp; 
  47.     //neighbor nodes array 
  48.     struct AStarCellNode * neighborArray[8]; 
  49.     //parent node 
  50.     struct AStarCellNode * lpParent; 
  51. } *pAStarCellNode,AStarCellNode_; 
  52.  
  53. //AStarMap for manager the cells. 
  54. typedef struct AStarMap 
  55.     // 1 means inited. 
  56.     char initState; 
  57.     int width; 
  58.     int height; 
  59.     char ** lpSourceMatrix; 
  60.     AStarCellNode_ *** lpCellMatrix; 
  61. }*pAStarMap,AStarMap_; 
  62.  
  63. /*********************************** 
  64.  *Openlist & Closelist Structures 
  65.  * 
  66.  ***********************************/ 
  67. typedef struct AStarCellListNode 
  68.     struct AStarCellListNode * prevNode; 
  69.     struct AStarCellListNode * nextNode; 
  70.     pAStarCellNode  currentCellNode; 
  71. }*pAStarCellListNode,AStarCellListNode_; 
  72.  
  73. typedef struct AStarOpenList 
  74.     int nodeCount; 
  75.     pAStarMap pMap; 
  76.     char ** assertMap; 
  77.     pAStarCellListNode headNode; 
  78.     pAStarCellListNode tailNode; 
  79. }*pAStarOpenList,AStarOpenList_; 
  80.  
  81. typedef struct AStarCloseList 
  82.     pAStarMap pMap; 
  83.     // choose char to instead of boolean type 
  84.     char ** assertMap; 
  85. }*pAStarCloseList,AStarCloseList_; 
  86.  
  87. /*************************** 
  88.  *other structure 
  89.  * 
  90.  **************************/ 
  91.  
  92. typedef struct AStarPoint 
  93.     int x; 
  94.     int y; 
  95. }*pAStarPoint,AStarPoint_; 
  96.  
  97. typedef struct AStarPathNode 
  98.     struct AStarPathNode * prevNode; 
  99.     struct AStarPathNode * nextNode; 
  100.     pAStarCellNode currentNode; 
  101. }*pAStarPathNode,AStarPathNode_; 
  102.  
  103. typedef struct AStarPathList 
  104.     pAStarPathNode headNode; 
  105.     pAStarPathNode tailNode; 
  106.     int nodesCount; 
  107. }*pAStarPathList,AStarPathList_; 
  108.  
  109. typedef float (* CalculateCallBack)(pAStarCellNode,pAStarCellNode,pAStarCellNode); 
  110. typedef struct AStarMain 
  111.     CalculateCallBack cal_fun; 
  112.     pAStarMap  pMap; 
  113.     pAStarCloseList pCloseList; 
  114.     pAStarOpenList pOpenlist; 
  115. }*pAStar,ASTAR,AStar; 
  116.  
  117.  
  118. /// 
  119. #include <math.h> 
  120.  
  121.  
  122.  
  123. // astar cell functions 
  124. pAStarCell astar_cell_init(float x_,float y_,short int cellType); 
  125.  
  126. //astar node functions 
  127. struct AStarCellNode * astar_cellNode_init(struct AStarCell * pCell_); 
  128.  
  129. void astar_cellNode_assignNeighborNodes( 
  130.                                     struct AStarCellNode * pNode, 
  131.                                     pAStarCellNode down_, 
  132.                                     pAStarCellNode left_, 
  133.                                     pAStarCellNode leftDown_, 
  134.                                     pAStarCellNode leftUp_, 
  135.                                     pAStarCellNode right_, 
  136.                                     pAStarCellNode rightDown_, 
  137.                                     pAStarCellNode rightUp_, 
  138.                                     pAStarCellNode up_); 
  139. void astar_cellNode_cleanup(struct AStarCellNode * pNode); 
  140. void astar_cellNode_reset(struct AStarCellNode * pNode); 
  141. // astar map functions 
  142. // 
  143. pAStarMap astar_map_initFromMemery(void * bytesBuffer,int width,int height); 
  144. void astar_map_printBytesMatrix(pAStarMap pMap); 
  145. void astar_map_cleanup(pAStarMap pMap); 
  146. /**************************** 
  147.  *Openlist & Closelist utilities 
  148.  * 
  149.  ****************************/ 
  150. pAStarCellListNode  astar_cellListNode_init(pAStarCellNode pCellNode); 
  151. void astar_cellListNode_cleanup(pAStarCellListNode pCellListNode); 
  152.  
  153. pAStarOpenList astar_openList_init(pAStarMap pMap); 
  154. void astar_openList_insertCellNode(pAStarOpenList pOpenList,pAStarCellNode pCellNode); 
  155. pAStarCellNode astar_openList_removeTheLeaseCostNode(pAStarOpenList pOpenList); 
  156. pAStarCellNode astar_openList_retriveTheLeaseCostNode(pAStarOpenList pOpenList); 
  157. void astar_openList_cleanup(pAStarOpenList pOpenList); 
  158. void astar_openList_printList(pAStarOpenList pOpenList); 
  159. void astar_openList_reset(pAStarOpenList pOpenList); 
  160.  
  161. pAStarCloseList astar_closeList_init(pAStarMap pMap); 
  162. void astar_closeList_addCellNode(pAStarCloseList pCloseList,pAStarCellNode pCellNode); 
  163. char astar_closeList_assertHasCellNode(pAStarCloseList pCloseList,pAStarCellNode pCellNode); 
  164. void astar_closeList_reset(pAStarCloseList pCloseList); 
  165. void astar_closeList_cleanup(pAStarCloseList pCloseList); 
  166.  
  167. /**************************** 
  168.  *main structures function* 
  169.  ****************************/ 
  170.  
  171. ASTAR * astar_main_initAStar(void * ptr,int width,int height); 
  172. void astar_main_cleanup(pAStar astar); 
  173. void astar_main_setCalculateCallBack(pAStar astar,CalculateCallBack callbackfun); 
  174.  
  175. /**************************** 
  176.  *path finding function* 
  177.  ****************************/ 
  178.  
  179. char astar_findPath(pAStar astar,AStarPoint_ start,AStarPoint_ end,pAStarPathList pPathOuter); 
  180.  
  181. /**************************** 
  182.  *path creating function* 
  183.  ****************************/ 
  184. pAStarPathList astar_pathList_init(void); 
  185. void astar_path_insertFront(pAStarPathList pathList,pAStarCellNode pCellNode); 
  186. void astar_path_print(pAStarPathList pPathList); 
  187. void astar_path_cleanup(pAStarPathList pPathList); 
  188.  
  189. /**************************** 
  190.  *the cost calculating function* 
  191.  ****************************/ 
  192. float astar_calc_diagonal(pAStarCellNode parentNode,pAStarCellNode currentNode,pAStarCellNode destinationNode); 
  193. float astar_calc_manhatton(pAStarCellNode parentNode,pAStarCellNode currentNode,pAStarCellNode destinationNode); 
  194. float astar_calc_euclidian(pAStarCellNode parentNode,pAStarCellNode currentNode,pAStarCellNode destinationNode); 
  195. #endif // ASTAR_H_INCLUDED 

 

main.c

 

 
  
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include "astar.h" 
  4. int main() 
  5.     char * mem = "\0\0\0\0\0\0\0\0\0\0\1\1\0\0\0\1\1\0\0\0\0\0\0\0\0\0\0\0\1\1\0\0\0\1\1\0\0\0\0\0\0\0\0\0\0"
  6.     ASTAR * astar = astar_main_initAStar(mem,9,5); 
  7.     //the callback function is used to calculate the cost. 
  8.     //and it has a callback function by default., three callback can be used in this lib 
  9.     //astar_main_setCalculateCallBack(astar,astar_calc_manhatton); 
  10.     astar_main_setCalculateCallBack(astar,astar_calc_euclidian); 
  11.     AStarPoint_ start; 
  12.     start.x = 0; 
  13.     start.y = 0; 
  14.     AStarPoint_ end; 
  15.     end.x = 8; 
  16.     end.y = 4; 
  17.  
  18.     AStarPathList_ * path = astar_pathList_init(); 
  19.  
  20.     astar_map_printBytesMatrix(astar->pMap); 
  21.  
  22.     if(astar_findPath(astar,start,end,path)) 
  23.     { 
  24.         printf("found the path successfully!"); 
  25.         astar_path_print(path); 
  26.         astar_path_cleanup(path); 
  27.     } 
  28.     else 
  29.     { 
  30.         printf("can not find the path!"); 
  31.     } 
  32.  
  33.     end.x = 2; 
  34.  
  35.     astar_path_cleanup(path); 
  36.     path= astar_pathList_init(); 
  37.  
  38.     if(astar_findPath(astar,start,end,path)) 
  39.     { 
  40.         printf("found the path successfully!"); 
  41.         astar_path_print(path); 
  42.         astar_path_cleanup(path); 
  43.     } 
  44.     else 
  45.     { 
  46.         printf("can not find the path!"); 
  47.     } 
  48.  
  49.     astar_main_cleanup(astar); 
  50.     return 0; 

整体来说astar算法较为简单,对算法熟悉者一两个小时就可以弄懂并实现.

而且基本的astar算法掌握好之后,可以对此进行扩展比如蜂窝状寻路, 三维寻路等等.都是轻而易举的事情.