数据结构和算法总结(三):A* 寻路算法
前言
复习下寻路相关的东西,而且A star寻路在游戏开发中应用挺多的,故记录下。
正文
迪杰斯特拉算法
说起A*得先谈谈Dijkstra算法,它是在BFS基础上的一种带权值的两点最短寻路贪心算法。
算法步骤
0.初始化图,输入起点,将所有点到起始点的距离设置为∞。
1.将起始点OriginNode记录为已访问,并从OriginNode开始将周围的点加入到待遍历列表中,更新到达这些点的距离,并将他们的父节点设置为起始点OriginNode。
2.如果待遍历列表不为空,则从列表中取出到达距离最小的点currentNode;反之则跳到第5步。
3.遍历currentNode邻接的点neighbors:
1) 如果已访问过,则遍历其他点。
2) 如果邻接的某点neighbor已经在待遍历列表中,则比较neighbor当前的距离distance(OriginNode,neighbor) 与 distance(OriginNode,currentNode) + distance(currentNode,neighbor), 如果后者更小,则将neighbor的距离值更新为该值并将父节点设置为currentNode。
3) 如果不在待遍历列表,则将neighbor放入待遍历列表,并则将neighbor的距离值更新为 distance(OriginNode,currentNode) + distance(currentNode,neighbor), 并将父节点设置为currentNode。
4.重复第2步
5.输入终点,从终点开始回溯父节点,打印路径。
动态过程
A* 寻路算法
A*的思路其实与Dijkstra一致,相比较Dijkstra,A*引入了一个启发公式来衡量消耗
其中 g(n) 代表从起始点到当前点的消耗,h(n) 代表终点到当前点的预估消耗(通常用曼哈顿距离或者欧拉距离衡量,这里不赘述,参考)。
算法思路
- /*
- A*寻路算法
- 带权值的BFS
- 每个点保存达到该点的消耗F = G值(出发点到当前点的代价值) + H值(目标点到当前点的预估距离,常用曼哈顿距离或者欧拉距离)
- 维护着两个列表,开放列表和关闭列表
- 逻辑:
- 初始化列表Close,Open,Path,将StartNode放入Open列表中
- while(true)
- {
- 从Open表中取得F值最小的节点CurrentNode.
- 从Open表中删除CurrentNode
- 将CurrentNode加入Close表中
- if ( CurrentNode 是 目标点targetNode)
- {
- 从CurrentNode点回溯直到起点并将所有回溯的节点加入Path表
- 打印Path表
- return;
- }
- for (CurrentNode 的每一个邻接点 neighbor)
- {
- if(neighbor 在 Close表中 || neighbor 不可通行)
- {
- continue;
- }
- if(neighbor 在 Open表中)
- {
- if( CurrentNode 到达neighbor的消耗costG + node的G值 < neighbor的G值)
- {
- 更新neighbor的G值;
- 将neighbor的回溯节点fatherNode设置为CurrentNode;
- }
- }
- else
- {
- 更新neighbor的G值;
- 更新neighbor的H值;
- 将neighbor的回溯节点fatherNode设置为CurrentNode;
- 将neighbor放入Open列表;
- }
- }
- }
- */
动态过程
代码:
AstarNode的定义实现.
Astar.h
- #pragma once
- #include <vector>
- #include <math.h>
- using std::vector;
- class AstarNode
- {
- private:
- int m_igValue;
- int m_ihValue;
- int m_ifValue;
- AstarNode* m_father;
- public:
- int x;
- int y;
- bool isPass;
- public:
- void SetG(int iValue);
- int GetG() const;
- int GetH() const;
- int GetF() const;
- void CalculateH(const AstarNode* endNode);
- void SetFather(AstarNode* node);
- AstarNode* GetFather() const;
- bool isInList(const vector<AstarNode*>& nodeList) const;
- public:
- AstarNode();
- AstarNode(int x,int y);
- AstarNode(const AstarNode& node);
- ~AstarNode();
- AstarNode& operator=(const AstarNode& node);
- };
Astar.cpp
- #include "Astar.h"
- AstarNode::AstarNode()
- {
- isPass = true;
- m_igValue = 0;
- m_ihValue = 0;
- m_ifValue = 0;
- m_father = nullptr;
- }
- AstarNode::AstarNode(int x,int y)
- {
- isPass = true;
- m_igValue = 0;
- m_ihValue = 0;
- m_ifValue = 0;
- m_father = nullptr;
- this->x = x;
- this->y = y;
- }
- AstarNode::AstarNode(const AstarNode& node)
- {
- isPass = node.isPass;
- m_igValue = node.GetG();
- m_ihValue = node.GetH();
- m_ifValue = node.GetF();
- m_father = node.GetFather();
- this->x = node.x;
- this->y = node.y;
- }
- AstarNode& AstarNode::operator=(const AstarNode& node)
- {
- isPass = node.isPass;
- m_igValue = node.GetG();
- m_ihValue = node.GetH();
- m_ifValue = node.GetF();
- m_father = node.GetFather();
- this->x = node.x;
- this->y = node.y;
- return *this;
- }
- AstarNode::~AstarNode()
- {
- }
- void AstarNode::SetG(int iValue)
- {
- m_igValue = iValue;
- }
- int AstarNode::GetG() const
- {
- return m_igValue;
- }
- int AstarNode::GetF() const
- {
- return m_igValue + m_ihValue;
- }
- int AstarNode::GetH() const
- {
- return m_ihValue;
- }
- void AstarNode::CalculateH(const AstarNode* endNode)
- {
- m_ihValue = abs(endNode->x - x) + abs(endNode->y - y);
- }
- void AstarNode::SetFather(AstarNode* node)
- {
- m_father = node;
- }
- AstarNode* AstarNode::GetFather() const
- {
- return m_father;
- }
- bool AstarNode::isInList(const vector<AstarNode*>& nodeList) const
- {
- for(auto iter = nodeList.begin();iter != nodeList.end();iter++)
- {
- if((*iter)->x == this->x && (*iter)->y == this->y)
- return true;
- }
- return false;
- }
main.cpp
- #include "Astar.cpp"
- #include <algorithm>
- using namespace std;
- /*
- 'e' 终点
- 's' 起点
- '#' 障碍物
- '-' 可通行点
- 'o' 回溯路径节点
- */
- vector<vector<char>> graph = {
- {'-', '-', '-', '-', '-', '-', '-'},
- {'-', '-', 'e', '#', '-', '-', '-'},
- {'-', '#', '#', '#', '#', '-', '-'},
- {'-', '-', '-', '-', '#', '#', '-'},
- {'-', '-', '#', '-', '#', '-', '-'},
- {'-', '-', '-', '#', '-', '-', '-'},
- {'-', '-', '-', '-', '-', '-', 's'},
- };
- void BuildGraph(vector<vector<AstarNode *>> &astarGraph, AstarNode &start, AstarNode &end)
- {
- for (int i = 0; i < astarGraph.size(); i++)
- {
- auto row = astarGraph[i];
- for (int j = 0; j < row.size(); j++)
- {
- char mark = graph[i][j];
- if (mark == 's')
- {
- start.x = i;
- start.y = j;
- start.SetG(0);
- start.CalculateH(&end);
- astarGraph[i][j] = &start;
- }
- else if (mark == 'e')
- {
- end.x = i;
- end.y = j;
- astarGraph[i][j] = &end;
- }
- else if (mark == '#')
- {
- astarGraph[i][j] = new AstarNode(i, j);
- astarGraph[i][j]->isPass = false;
- }
- else
- {
- astarGraph[i][j] = new AstarNode(i, j);
- }
- }
- }
- }
- void printGraph()
- {
- for (int i = 0; i < graph.size(); i++)
- {
- auto line = graph[i];
- for (int j = 0; j < line.size(); j++)
- {
- cout << line[j] << " ";
- }
- cout << endl;
- }
- }
- vector<AstarNode *>::iterator GetMinFNode(vector<AstarNode *> &openList)
- {
- auto tmp = openList.begin();
- for (auto iter = openList.begin(); iter != openList.end(); iter++)
- {
- if ((*iter)->GetF() < (*tmp)->GetF())
- {
- tmp = iter;
- }
- }
- return tmp;
- }
- inline int GetCost(int xDiff, int yDiff)
- {
- if (xDiff == 0 || yDiff == 0)
- return 10;
- return 14;
- }
- void SearchOneNode(AstarNode ¤tNode, AstarNode &neighbor, AstarNode &startNode, AstarNode &endNode, vector<AstarNode *> &openList, vector<AstarNode *> &closeList)
- {
- if (neighbor.isInList(closeList) || !neighbor.isPass)
- return;
- int gCost = GetCost(currentNode.x - neighbor.x, currentNode.y - neighbor.y);
- if (neighbor.isInList(openList))
- {
- if (currentNode.GetG() + gCost < neighbor.GetG())
- {
- neighbor.SetG(currentNode.GetG() + gCost);
- neighbor.SetFather(¤tNode);
- }
- }
- else
- {
- neighbor.SetG(currentNode.GetG() + gCost);
- neighbor.SetFather(¤tNode);
- neighbor.CalculateH(&endNode);
- openList.push_back(&neighbor);
- }
- }
- void Astar()
- {
- vector<AstarNode *> OpenList;
- vector<AstarNode *> CloseList;
- vector<AstarNode *> Path;
- size_t len = graph.size();
- vector<vector<AstarNode *>> astarGraph(len, vector<AstarNode *>(len));
- AstarNode startNode;
- AstarNode endNode;
- BuildGraph(astarGraph, startNode, endNode);
- OpenList.push_back(&startNode);
- while (!OpenList.empty())
- {
- auto it = GetMinFNode(OpenList);
- AstarNode *currentNode = *it;
- OpenList.erase(it);
- CloseList.push_back(currentNode);
- if (currentNode->x == endNode.x && currentNode->y == endNode.y)
- {
- while (currentNode->GetFather())
- {
- graph[currentNode->x][currentNode->y] = graph[currentNode->x][currentNode->y] == '-' ? 'o' : 'e';
- Path.push_back(currentNode);
- currentNode = currentNode->GetFather();
- }
- printGraph();
- break;
- }
- int curX = currentNode->x;
- int curY = currentNode->y;
- int row = graph.size();
- int column = row;
- if (curX + 1 < row)
- SearchOneNode(*currentNode, *astarGraph[curX + 1][curY], startNode, endNode, OpenList, CloseList);
- if (curX - 1 >= 0)
- SearchOneNode(*currentNode, *astarGraph[curX - 1][curY], startNode, endNode, OpenList, CloseList);
- if (curY + 1 < column)
- SearchOneNode(*currentNode, *astarGraph[curX][curY + 1], startNode, endNode, OpenList, CloseList);
- if (curY - 1 >= 0)
- SearchOneNode(*currentNode, *astarGraph[curX][curY - 1], startNode, endNode, OpenList, CloseList);
- if (curX - 1 >= 0 && curY - 1 >= 0)
- SearchOneNode(*currentNode, *astarGraph[curX - 1][curY - 1], startNode, endNode, OpenList, CloseList);
- if (curX - 1 >= 0 && curY + 1 < column)
- SearchOneNode(*currentNode, *astarGraph[curX - 1][curY + 1], startNode, endNode, OpenList, CloseList);
- if (curX + 1 < row && curY - 1 >= 0)
- SearchOneNode(*currentNode, *astarGraph[curX + 1][curY - 1], startNode, endNode, OpenList, CloseList);
- if (curX + 1 < row && curY + 1 < row)
- SearchOneNode(*currentNode, *astarGraph[curX + 1][curY + 1], startNode, endNode, OpenList, CloseList);
- }
- cout << endl;
- }
- int main()
- {
- auto start = chrono::steady_clock::now();
- Astar();
- auto end = chrono::steady_clock::now();
- cout << chrono::duration<double, milli>(end - start).count() << " ms" << endl;
- getchar();
- }
参考资料
数据结构和算法总结(三):A* 寻路算法的更多相关文章
- 【数据结构】 最小生成树(三)——prim算法
上一期介绍到了kruskal算法,这个算法诞生于1956年,重难点就是如何判断是否形成回路,此处要用到并查集,不会用当然会觉得难,今天介绍的prim算法在kruskal算法之后一年(即1957年)诞生 ...
- 人工智能: 自动寻路算法实现(三、A*算法)
博客转载自:https://blog.csdn.net/kwame211/article/details/78139506 本篇文章是机器人自动寻路算法实现的第三章.我们要讨论的是一个在一个M×N的格 ...
- [转] A*寻路算法C++简单实现
参考文章: http://www.policyalmanac.org/games/aStarTutorial.htm 这是英文原文<A*入门>,最经典的讲解,有demo演示 http: ...
- 【数据结构】 最小生成树(二)——kruskal算法
上一期说完了什么是最小生成树,这一期咱们来介绍求最小生成树的算法:kruskal算法,适用于稀疏图,也就是同样个数的节点,边越少就越快,到了数据结构与算法这个阶段了,做题靠的就是速度快,时间复杂度小. ...
- [A*算法]基于Unity实现A*算法(二)
写在前面:上一篇当时是非常简单的了解一下A*,昨天还有一些问题没解决,就暂时把自己查阅的文坛摘抄了过来(毕竟人家写的比我要好的多 :> ) 今天终于解决了,就又写了这一篇,正好我自己再梳理一遍, ...
- 图论算法(四)Dijkstra算法
最短路算法(三)Dijkstra算法 PS:因为这两天忙着写GTMD segment_tree,所以博客可能是seg+图论混搭着来,另外segment_tree的基本知识就懒得整理了-- Part 1 ...
- A*寻路算法的探寻与改良(三)
A*寻路算法的探寻与改良(三) by:田宇轩 第三分:这部分内容基于树.查找算法等对A*算法的执行效率进行了改良,想了解细 ...
- Java数据结构和算法(三)顺序存储的树结构
Java数据结构和算法(三)顺序存储的树结构 二叉树也可以用数组存储,可以和完全二叉树的节点一一对应. 一.树的遍历 // 二叉树保存在数组中 int[] data; public void preO ...
- 重读《学习JavaScript数据结构与算法-第三版》- 第4章 栈
定场诗 金山竹影几千秋,云索高飞水自流: 万里长江飘玉带,一轮银月滚金球. 远自湖北三千里,近到江南十六州: 美景一时观不透,天缘有分画中游. 前言 本章是重读<学习JavaScript数据结构 ...
随机推荐
- VUE项目开发流程
前期准备 安装npm 安装webpack\vue-cli(2.9.6版本--版本不同可能会导致以下一些目录结构以及错误解决办法不符合实际情况) 创建项目 初始化创建项目,项目名称.项目描述.拥有者等等 ...
- ICEM—二维混合网格对齐节点
原视频下载地址: https://pan.baidu.com/s/1bpnjfT9 密码: jeuv
- Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤
Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤 一.在ubuntu下尝试 1. 在虚拟机VWware Workstation 12. ...
- WINRAR弹窗堆栈
0:000> db 004ddfa8004ddfa8 6f 00 70 00 65 00 6e 00-00 00 00 00 2d 00 6e 00 o.p.e.n.....-.n. 03063 ...
- php-fpm(绕过open_basedir,结合ssrf)
环境的安装->https://www.cnblogs.com/zaqzzz/p/11870489.html 1.nginx的畸形访问 因为安装的是php7.0,所以需要手动修改一下(版本低的时候 ...
- vue实现购物清单列表添加删除
vue实现购物清单列表添加删除 一.总结 一句话总结: 基础的v-model操作,以及数组的添加(push)删除(splice)操作 1.checkbox可以绑定数组,也可以直接绑定值? 绑定数组就是 ...
- mysql安装到启动遇见的问题
一.有时候安装mysql后使用mysql命令时报错 Can't connect to MySQL server on localhost (10061),或者用net start mysql 时报服务 ...
- python连接mysql数据库(MySQL)
在介绍python在数据库中的操作之前先简单介绍点mysql中的操作语言: [root@elk-server --]# mysql -u root -p Enter password: Welcome ...
- Visual Studio Code 上java开发环境搭建
在把一些开源的SDK中java代码转成C#代码时经常需要写点java代码来实验下功能,装个Eclipse或IDEAs吧,好像也不太值当,所以用vs code搭个环境偶尔来实验下.以下: 1.下载并装好 ...
- 相机用的 SD Card 锁Lock 烂掉了,无法正常写入
没错,又碰到奇奇怪怪的SD Card Lock 烂掉了 , 无法正常写入,不要急,千万不要扔了,拿起透明胶粘在 Lock 处,注意不要粘住金手指哦,再放回去就可以读写了,但是透明胶不耐摩擦,用了几次 ...