当前位置:   article > 正文

A_Star算法C++实现_astar c++

astar c++

关于A星算法,在这里提两个关键点:

   (1).适应用于静态场景。A星算法需要提前构建场景地图(SLAM),在此基础之上做路径规划。

   (2).启发式搜索。采用位置评估手段计算路径代价。比如我们要用的曼哈顿距离。

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. #include <vector>
  5. #include <cmath>
  6. #define N 6 // 棋盘/迷宫 的阶数
  7. using namespace std;
  8. class Node {
  9. public:
  10. int x, y; // 节点坐标
  11. //f=g+h,总代价;
  12. //g,上个点到该点的代价;
  13. //h,该点到终点的估计值
  14. int f, g, h;
  15. Node(int a, int b) {
  16. this->x = a;
  17. this->y = b;
  18. }
  19. //重载'<'运算符,因为队列不知道怎么对Node排序
  20. bool operator < (const Node& a)const {
  21. //优先队列是大顶堆,因此这里反过来成为小顶堆。Set是小顶堆,如果用Set,这里的判定用'<'
  22. return this->f > a.f;
  23. }
  24. };
  25. bool visit[N][N]; //记录节点是否访问过
  26. int path[N][N][2]; //记录父节点坐标,用于找路径
  27. int realF[N][N]; //记录节点的实际f值
  28. int addr[N][N]= { {0,0,0,0,0,0},//环境二维矩阵
  29. {0,1,1,0,1,1},
  30. {0,0
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/848839
推荐阅读
相关标签
  

闽ICP备14008679号