赞
踩
关于A星算法,在这里提两个关键点:
(1).适应用于静态场景。A星算法需要提前构建场景地图(SLAM),在此基础之上做路径规划。
(2).启发式搜索。采用位置评估手段计算路径代价。比如我们要用的曼哈顿距离。
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <vector>
- #include <cmath>
- #define N 6 // 棋盘/迷宫 的阶数
- using namespace std;
- class Node {
- public:
- int x, y; // 节点坐标
- //f=g+h,总代价;
- //g,上个点到该点的代价;
- //h,该点到终点的估计值
- int f, g, h;
- Node(int a, int b) {
- this->x = a;
- this->y = b;
- }
- //重载'<'运算符,因为队列不知道怎么对Node排序
- bool operator < (const Node& a)const {
- //优先队列是大顶堆,因此这里反过来成为小顶堆。Set是小顶堆,如果用Set,这里的判定用'<'
- return this->f > a.f;
- }
- };
- bool visit[N][N]; //记录节点是否访问过
- int path[N][N][2]; //记录父节点坐标,用于找路径
- int realF[N][N]; //记录节点的实际f值
- int addr[N][N]= { {0,0,0,0,0,0},//环境二维矩阵
- {0,1,1,0,1,1},
- {0,0
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreBlack.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。