A星寻路算法的讲解有很多,这里不再论述,只给出实现程序。

AStar.h

#pragma once#include <vector>
#include <map>
#include <queue>
// 二维地图A*算法实现const int OBLIQUE = 14;  // 斜线移动权重为14
const int STEP = 10;
struct Point
{Point(int id, int reachable);~Point();void CalcF() { m_F = m_G + m_H; }bool IsReachable(){if (m_reachable == 0) return true;return false;}bool operator>(const Point* point) // 优先队列小顶堆{if (m_F > point->m_F){return true;}return false;}int m_F{ 0 };  // F = G + Hint m_G{ 0 };  // G 表示从起点 A 移动到网格上指定方格的移动耗费 (可沿斜方向移动)int m_H{ 0 };  // H 表示从指定的方格移动到终点 B 的预计耗费 (H 有很多计算方法, 这里我们设定只可以上下左右移动)int m_id; // 二维数组转换成一维数组的位置编号 0, 1, 2,....int m_reachable;  // 0 可达 1 不可达Point* m_parent{nullptr};  // 保存父节点int m_close{0};  // 0 未加入closelist 1 加入closelistint m_x{0};int m_y{0};
};//--------------
//|0 1 3 4 5
//|6 7 8 9 10
//|11 12 13 14 15
//|....
typedef std::vector<Point*> point_vec_type;
typedef std::map<int, Point*> point_map_type;
typedef std::priority_queue<Point*, point_vec_type> point_queue_type;
class AStarNav
{
public:AStarNav();~AStarNav();bool FindPath(int start_id, int end_id, std::vector<Point*>& find_path);bool CanReach(int start_id, int end_id);bool LoadMap(const char* path);int GetPointId(int i, int j);
private:void GetAroundPoints(int point_id, point_vec_type& point_set);void GetPos(int& i, int& j, int point_id);bool StopSearch(int target_id);Point* GetPoint(int point_id);Point* GetMinFInOpenList();template <typename T>bool IsInList(const T& t, Point* point);void InOpenList(Point* point);void OutOpenList(Point* point);int CalcG(const Point* start, const Point* point);int CalcH(const Point* end, const Point* point);
private:point_map_type m_close_list;  // 检查完毕列表point_map_type m_open_list;   // 待检查列表point_queue_type m_open_queue; // 查找最小值的辅助结构point_map_type m_points; // 地图所以格子int m_map_width{ 0 };int m_map_hight{ 0 };
};

Astar.cpp

#include <fstream>
#include <string>
#include <algorithm>
#include "AStarNav.h"
#include "lexical_cast.hpp"
#include "StringUtil.h"Point::Point(int id, int reachable):m_id(id),m_reachable(reachable)
{
}Point::~Point()
{
}AStarNav::AStarNav()
{}AStarNav::~AStarNav()
{for (auto it : m_points){delete it.second;it.second = nullptr;}m_close_list.clear();m_open_list.clear();m_points.clear();
}bool AStarNav::LoadMap(const char* path)
{std::ifstream ifs(path);if (!ifs) return false;std::string line;int point_id = 0;while (!ifs.eof()){ifs >> line;std::vector<std::string>  vec_nodes;StringUtil::SplitCpp(line, ",", vec_nodes);if (m_map_width == 0){m_map_width = (int)vec_nodes.size();}for (auto& it : vec_nodes){int k = lexical_cast<int>(it);Point* point = new Point(point_id, k);point->m_x = point_id % m_map_width;point->m_y = m_map_hight;m_points.emplace(point_id, point);point_id++;}m_map_hight++;}ifs.close();return true;
}void AStarNav::GetPos(int& i, int& j, int point_id)
{i = point_id % m_map_width;j = point_id / m_map_hight;
}int AStarNav::GetPointId(int i, int j)
{int point_id = j * m_map_width + i;return point_id;
}void AStarNav::GetAroundPoints(int point_id, point_vec_type& point_vec)
{int i = 0, j = 0;GetPos(i, j, point_id);Point* point = nullptr;for(int x = i-1; x<=i+1; x++)for (int y = j - 1; y <= j + 1; y++){if (x >= m_map_width || x < 0) continue;if (y >= m_map_hight || y < 0) continue;Point* point = GetPoint(GetPointId(x, y));if (point && point->IsReachable() && point->m_id != point_id){point_vec.emplace_back(point);}}
}bool AStarNav::FindPath(int start_id, int end_id, std::vector<Point*>& find_path)
{if (CanReach(start_id, end_id)){Point* start = GetPoint(start_id);Point* end = GetPoint(end_id);Point* point = end;while (point->m_parent){find_path.emplace_back(point);point = point->m_parent;}find_path.emplace_back(start);  // 路径包含了开始结束位置return true;}return false;
}bool AStarNav::CanReach(int start_id, int end_id)
{m_open_list.clear();m_close_list.clear();Point* start = GetPoint(start_id);Point* end = GetPoint(end_id);if (start == nullptr || end == nullptr)return false;InOpenList(start);point_vec_type around_points;while (m_open_list.size() > 0){Point* tmp_start = GetMinFInOpenList();m_close_list.emplace(tmp_start->m_id, tmp_start);OutOpenList(tmp_start);around_points.clear();GetAroundPoints(tmp_start->m_id, around_points);for (auto point : around_points){if (!point->IsReachable() || IsInList<point_map_type>(m_close_list, point))continue;if (!IsInList<point_map_type>(m_open_list, point)){point->m_parent = tmp_start;point->m_G = CalcG(tmp_start, point);point->m_H = CalcH(end, point);m_open_list.emplace(point->m_id,point);m_open_queue.push(point);}else{int G = CalcG(tmp_start, point);if (G < point->m_G){point->m_parent = tmp_start;point->m_G = G;point->CalcF();}}}if (IsInList<point_map_type>(m_open_list, end)){return true;}}return false;
}Point* AStarNav::GetMinFInOpenList()
{return m_open_queue.top();
}// 目标格已经在 "开启列表", 这时候路径被找到
bool AStarNav::StopSearch(int target_id)
{auto it = m_open_list.find(target_id);if (it != m_open_list.end()){return true;}return false;
}
void AStarNav::InOpenList(Point* point)
{m_open_list.emplace(point->m_id, point);m_open_queue.push(point);
}
void AStarNav::OutOpenList(Point* point)
{m_open_queue.pop();m_open_list.erase(point->m_id);
}Point* AStarNav::GetPoint(int point_id)
{auto it = m_points.find(point_id);if (it == m_points.end()){return nullptr;}return it->second;
}template <typename T>
bool AStarNav::IsInList(const T& t, Point* point)
{auto it = t.find(point->m_id);if (it != t.end())return true;return false;
}int AStarNav::CalcG(const Point* start, const Point* point)
{ int G = (abs(point->m_x - start->m_x) + abs(point->m_y - start->m_y)) == 2 ? OBLIQUE: STEP;int parentG = point->m_parent != nullptr ? point->m_parent->m_G : 0;return G + parentG;
}int AStarNav::CalcH(const Point* end, const Point* point)
{int step = abs(point->m_x - end->m_x) + abs(point->m_y - end->m_y);return step * STEP;
}

原理很简单就是将寻路点加到open list,计算路径权重,如果已经在close list 重新计算权重,然后在open list的选出最小的权重作为开始节点,然后重复的加入周边的节点到open list, 这个节点放入close list中。

这里将地图格子看作一个一个点,测试map:

0,0,0,0,0,0,0,0,0,0
0,0,0,1,0,0,1,0,0,0
0,0,0,1,0,1,1,1,1,0
0,1,0,1,0,0,1,0,0,0
0,1,0,0,0,0,1,0,0,0
0,1,0,0,0,0,1,0,0,0
0,1,0,1,0,0,0,0,0,0
0,0,0,0,0,0,0,1,0,0
0,0,0,1,1,1,1,1,0,0
0,0,0,0,0,0,0,0,0,0

这里的01 既可以代码方格,也可以代码三角网格。三角形的网格只需要节点信息,稍微处理一下就可以。

c++完整实现地图寻路A星算法相关推荐

  1. 地图信息,障碍判断以及寻路算法(A星算法,B星算法和蚁群算法等)

    一.广度优先遍历和深度优先遍历 在学习寻路算法之前,我们先来了解一下广度优先遍历和深度优先遍历. 什么是广度优先遍历? 广度优先遍历(breadth first search)是一个万能的算法. 广度 ...

  2. 【路径规划】基于A星算法实现栅格地图路径规划

    一.简介 1.1搜索区域(The Search Area) 我们假设某人要从 A 点移动到 B 点,但是这两点之间被一堵墙隔开.如图 1 ,绿色是 A ,红色是 B ,中间蓝色是墙. ​ 图 1 你应 ...

  3. 【A星算法】A星寻路算法详解(小白也可以看懂+C#代码+零基础学习A*)

    1.问题背景 在制作RPG游戏角色和NPC移动时,需要角色自动避开障碍物,到达终点 怎么快速找到一条到达终点的路径? 使用a星寻路算法 2.A星算法的思路 绿色:起点:红色:终点 :黑色:障碍物 新增 ...

  4. 【无人机】基于A星算法实现三维栅格地图路径规划matlab代码

    1 算法介绍 A*搜寻算法俗称A星算法.这是一种在图形平面上,有多个节点的路径,求出最低通过成本的算法.常用于游戏中的NPC的移动计算,或线上游戏的BOT的移动计算上.(拷自百度百科)是常用搜索算法中 ...

  5. Cocos2d-x 3.1.1 学习日志16--A星算法(A*搜索算法)学问

    A *搜索算法称为A星算法.这是一个在图形平面,路径.求出最低通过成本的算法. 经常使用于游戏中的NPC的移动计算,或线上游戏的BOT的移动计算上. 首先:1.在Map地图中任取2个点,開始点和结束点 ...

  6. python 战棋游戏代码实现(2):六边形地图寻路和显示

    python 战棋游戏代码实现(2):六边形地图寻路和显示 六边形地图介绍 代码介绍 地图六边形显示 A*算法的六边形寻路修改 判断某个点在哪个六边形中 完整代码 编译运行 六边形地图介绍 之前的文章 ...

  7. 【A星算法的优化方案】

    当地图很大的时候,或者使用A星算法的寻路频率很高的时候,普通的A星算法就会消耗大量的CPU性能急剧下降,普通的A星性能还是不过关.接下来我们讲讲A星寻路在遇到性能瓶颈时的优化方案. 一.长距离导航 当 ...

  8. Java游戏服务器开发之A星算法

    Java游戏服务器开发之A星算法    学习这个主要是用于寻路算法.    参考资料主要是siki学院的视频,A计划--人工智能--A星算法. 网址http://www.sikiedu.com/cou ...

  9. 魔塔之拯救白娘子~我的第一个VB6+DX8做的小游戏源码~16开始游戏-自动寻路(A星算法)

    魔塔之拯救白娘子 完整工程下载地址: <魔塔之拯救白娘子>流程分析2: ⑤游戏界面鼠标点击判断以及自动寻路: 自动寻路的效果如下: 源码如下: Sub 游戏界面鼠标点击判断() Dim m ...

最新文章

  1. C++ 中 const和define的区别
  2. 转: 如何从keystore file中查看数字证书信息
  3. 如何让你瞬间拥有百万粉丝 前端F12的那些装X小技巧
  4. RPC实现Provider服务端业务逻辑
  5. CodeSmith注册机,支持5.2.2和5.2.1版
  6. 【转】RabbitMQ六种队列模式-2.工作队列模式
  7. android 中在CMD中查看sqlite
  8. 烹佛烹祖大炉鞴,锻凡锻圣恶钳锤
  9. java表示非法参数的异常是_JAVA 的异常那些事
  10. c语言需要什么硬件基础知识,学习c语言需要什么 基础c语言需要这些知识
  11. 用Python多线程抓取并验证代理(转)
  12. 讲清楚之 javascript原形
  13. macOS 升级12.6后 Electron 应用闪退
  14. Apache Calcite入门
  15. 分布式大气监测系统架构介绍及案例解析
  16. dct变换编码研究课设实验报告_制作电磁铁实验报告单_相关文章专题_写写帮文库...
  17. 3975: 人工智能(障)?
  18. ab的plc跟西门子哪个好些_周报61期 | 西门子全系列及博图软件常见问题解答
  19. Java 并发编程之美:并发编程高级篇之一-chat
  20. 淘宝网店装修模板尺寸大小及格式大全

热门文章

  1. 小程序技术助力开放银行建设
  2. 什么时候进入老年代?
  3. html如何用一行代码创建表格,HTML 表格练习题
  4. Qt之拦截关闭窗口的QCloseEvent简单使用
  5. 丹丹病了,我有钱一定借给你
  6. cetus权限连接主从mysql_cetus/cetus-quick-try.md at master · session-replay-tools/cetus · GitHub...
  7. 谷歌guava工具包详解
  8. 江西宜春1家公司发生爆燃致4死3伤-爆燃-车间倒塌
  9. flutter 颜色值处理
  10. ubuntu上python编辑器_Ubuntu中安装python编辑器Ulipad