最短路径

将地图存成二维数组,通过行列查找;

每一步都查找周围四个方向,或者八方向的所有点,放入开表;

  • 是否边缘
  • 是否障碍
  • 是否已经在确定的路线中

计算每个方向上路径消耗,一般斜着走消耗小,收益大;

开表排序,筛选出最小消耗的点放入闭表,并将该点设置为新的起点,从开表中去除该点;

重复上面操作,直到起点=终点退出;

中间查找八个方向可移动点位时,需要将这些可移动点的父节点设置为起点,这样可通过最后筛选出的点,一层层找出最短路径;

x步内可移动所有点位

将所有方向上可以移动的节点放入开表,同时放入闭表;

遍历闭表,将每个节点周围可移动的节点放入开表,同时放入新表;

清空闭表,新表赋值给闭表(实际代码不这么写);

重复上面操作,可移动几步,就循环几次;

最终返回开表;

代码实现:

public enum ObsType
{
None,
Wall,
Water,
Cliff
} public class AStarNode
{
public ObsType type;
public int x;
public int y;
public float g;
public float h;
public float f;
public AStarNode father;
public AStarNode(int _x,int _y,ObsType _type)
{
type = _type;
x = _x;
y = _y;
}
} [Serializable]
public class GridPos
{
public GridPos(int col, int row)
{
Col = col;
Row = row;
}
public int Col;
public int Row;
} public class AStarMgr
{
//地图的宽高
private int mapRow;
private int mapCol;
//地图相关的所有的格子容器
public AStarNode[,] nodes;
//开启列表
private List<AStarNode> openList = new List<AStarNode>();
//关闭列表
private List<AStarNode> closeList = new List<AStarNode>(); //初始化地图信息
public void InItMapInfo(int col, int row)
{
this.mapCol = col;
this.mapRow = row; nodes = new AStarNode[col, row];
for (int i = 0; i < col; i++)
{
for (int j = 0; j < row; j++)
{
AStarNode node = new AStarNode(i, j,
Random.Range(0, 100) < 20 ? ObsType.Wall : Ob
nodes[i, j] = node;
}
}
} //寻路的方法
public List<AStarNode> FindPath(GridPos startPos, GridPos end
{
//判断起始点是不是在地图的范围内
if (startPos.Col < 0 || startPos.Col >= mapCol
|| startPos.Row < 0 || startPos.Row
|| endPos.Col < 0 || endPos.Col >= m
|| endPos.Row < 0 || endPos.Row >= m
)
return null;
//判断起始点是不是不能通行的点
AStarNode start = nodes[startPos.Col, startPos.Row];
AStarNode end = nodes[endPos.Col, endPos.Row];
if (start.type != ObsType.None || end.type != ObsType.Non
return null;
closeList.Clear();
openList.Clear();
//开始点放入关闭列表中
start.father = null;
start.f = 0;
start.g = 0;
start.h = 0;
closeList.Add(start);
while (true)
{
//周围的点
//FindNearlyToOpenList(start.x - 1, start.y - 1, 1.4f
FindNearlyToOpenList(start.x, start.y - 1, 1.4f, star
//FindNearlyToOpenList(start.x + 1, start.y - 1, 1.4f
FindNearlyToOpenList(start.x - 1, start.y, 1.4f, star
FindNearlyToOpenList(start.x + 1, start.y, 1.4f, star
//FindNearlyToOpenList(start.x - 1, start.y + 1, 1.4f
FindNearlyToOpenList(start.x, start.y + 1, 1.4f, star
//FindNearlyToOpenList(start.x + 1, start.y + 1, 1.4f
if (openList.Count == 0)
return null;
//排序选出最小的点
openList.Sort(SortOpenList);
//放入关闭列表,然后从开启列表中移除
closeList.Add(openList[0]);
//找到这个点,进行下一次寻路
start = openList[0];
openList.RemoveAt(0);
if (start == end)
{
//结束
List<AStarNode> path = new List<AStarNode>();
path.Add(end);
while (end.father != null)
{
path.Add(end.father);
end = end.father;
}
path.Reverse();
return path;
}
}
}
//找出周围几步之内可到达的格子
public List<AStarNode> FindRange(GridPos startPos, int step)
{
if (startPos.Col < 0 || startPos.Col >= mapCol
|| startPos.Row < 0 || startPos.Row
return null;
AStarNode start = nodes[startPos.Col, startPos.Row];
if (start.type != ObsType.None)
return null;
closeList.Clear();
openList.Clear();
closeList.Add(start);
while (step-- > 0)
{
AddNearOneStep(closeList.ToArray());
}
return openList;
} //排序函数
private int SortOpenList(AStarNode a, AStarNode b)
{
if (a.f > b.f)
return 1;
else if (a.f == b.f)
return 1;
else
return -1;
} //临近的点放入开启列表
private void FindNearlyToOpenList(int x, int y, float g, ASta
{
if (x < 0 || x >= mapCol || y < 0 || y >= mapRow)
return;
AStarNode node = nodes[x, y];
if (node == null || node.type != ObsType.None
|| closeList.Contains(node)
|| openList.Contains(node)
)
return;
//计算f值 f=g+h;
node.father = father;
node.g = father.g + g;
node.h = Mathf.Abs(end.x - node.x) + Mathf.Abs(end.y - no
node.f = node.g + node.h;
openList.Add(node);
}
//当前步数的所有Node放入open
private void AddNearOneStep(AStarNode[] curStepNodes)
{
closeList.Clear();
foreach (var it in curStepNodes)
{
AddNearNode(it.x - 1, it.y);
AddNearNode(it.x, it.y - 1);
AddNearNode(it.x + 1, it.y);
AddNearNode(it.x, it.y + 1);
}
} //单个格子周围的点加入open
private void AddNearNode(int x, int y)
{
if (x < 0 || x >= mapCol || y < 0 || y >= mapRow)
return;
AStarNode node = nodes[x, y];
if (node == null || node.type != ObsType.None)
return;
openList.Add(node);
closeList.Add(node);
}
}

Unity-A-Star寻路算法的更多相关文章

  1. Unity实现A*寻路算法学习1.0

    一.A*寻路算法的原理 如果现在地图上存在两点A.B,这里设A为起点,B为目标点(终点) 这里为每一个地图节点定义了三个值 gCost:距离起点的Cost(距离) hCost:距离目标点的Cost(距 ...

  2. Unity实现A*寻路算法学习2.0

    二叉树存储路径节点 1.0中虽然实现了寻路的算法,但是使用List<>来保存节点性能并不够强 寻路算法学习1.0在这里:https://www.cnblogs.com/AlphaIcaru ...

  3. A星寻路算法入门(Unity实现)

    最近简单学习了一下A星寻路算法,来记录一下.还是个萌新,如果写的不好,请谅解.Unity版本:2018.3.2f1 A星寻路算法是什么 游戏开发中往往有这样的需求,让玩家控制的角色自动寻路到目标地点, ...

  4. 基于Unity的A星寻路算法(绝对简单完整版本)

    前言 在上一篇文章,介绍了网格地图的实现方式,基于该文章,我们来实现一个A星寻路的算法,最终实现的效果为: 项目源码已上传Github:AStarNavigate 在阅读本篇文章,如果你对于里面提到的 ...

  5. A*(也叫A star, A星)寻路算法Java版

    寻路算法有非常多种,A*寻路算法被公觉得最好的寻路算法. 首先要理解什么是A*寻路算法,能够參考这三篇文章: http://www.gamedev.net/page/resources/_/techn ...

  6. 算法:Astar寻路算法改进

    早前写了一篇<RCP:gef智能寻路算法(A star)> 出现了一点问题. 在AStar算法中,默认寻路起点和终点都是N x N的方格,但如果用在路由上,就会出现问题. 如果,需要连线的 ...

  7. 无递归 A星寻路算法

    整理硬盘的时候,发现我早些年写的A星寻路算法.特放上来,待有缘人拿去,无递归噢,性能那是杠杠的. 码上伺候 public class Node { public int X { get; set; } ...

  8. JS算法之A*(A星)寻路算法

    今天写一个连连看的游戏的时候,接触到了一些寻路算法,我就大概讲讲其中的A*算法. 这个是我学习后的一点个人理解,有错误欢迎各位看官指正. 寻路模式主要有三种:广度游戏搜索.深度优先搜索和启发式搜索. ...

  9. 一种高效的寻路算法 - B*寻路算法

    在此把这个算法称作B* 寻路算法(Branch Star 分支寻路算法,且与A*对应),本算法适用于游戏中怪物的自动寻路,其效率远远超过A*算法,经过测试,效率是普通A*算法的几十上百倍. 通过引入该 ...

  10. 数据结构和算法总结(三):A* 寻路算法

    前言 复习下寻路相关的东西,而且A star寻路在游戏开发中应用挺多的,故记录下. 正文 迪杰斯特拉算法 说起A*得先谈谈Dijkstra算法,它是在BFS基础上的一种带权值的两点最短寻路贪心算法. ...

随机推荐

  1. clickhouse智能提示编辑器

    对于经常写sql的人来说智能提示是非常重要的,这个非常影响写sql的效率和心情. 这里说的智能提示不仅仅是关键字(select等)的智能提示,还得要做到表字段的智能提示. 例如: 下面是mysql的智 ...

  2. Blazor WebAssembly 渐进式 Web 应用程序 (PWA) 使用 LocalStorage 离线处理数据

    原文链接:https://www.cnblogs.com/densen2014/p/16133343.html Window.localStorage 只读的localStorage 属性允许你访问一 ...

  3. Jx.Cms开发笔记(五)-文章编辑页面标签设计

    标签页的样子 设计思路 与其他输入框一样,存在一个Label标签,由于这里不像其他输入框一样可以直接使用Row标签,所以这里需要额外增加. 使用Tag组件显示所有的标签,我们在Blazor 组件库 B ...

  4. 小米电视去广告之adb实战

    近日闲来无事,对小米电视的开机广告.系统内置应用决定进行一波优化 安卓系统大部分都有一个"开发者模式", 在这个模式下可以放开手脚对系统进行一系列操作 此次要针对小米电视的UI使用 ...

  5. Bugku练习题---MISC---蜜雪冰城~

    Bugku练习题---MISC---蜜雪冰城~ flag:flag{1251_521_m1xueb1n9chen9ti@nm1mi} 解题步骤: 1.观察题目,下载附件 2.拿到手以后发现有好几个文件 ...

  6. Java 练手项目

    HotelSystem:https://github.com/misterchaos/HotelSystem(酒店管理系统 Java,tomcat,mysql,servlet,jsp实现,没有使用任何 ...

  7. sa-token server认证逻辑

    sa-token client登录逻辑

  8. 5.文件共享总结中篇-Linux服务器文件共享

    今天我们来讨论Linux系统之间的文件共享,包含:SCP.FTP.rz / sz.wget 一.SCP 其实我在写SSH协议常见问题排错章节中已介绍其SCP用法. 现我把SCP常用语法格式,给大家说下 ...

  9. Docker系列教程02-操作Docker容器

    简介 通过前面的学习,相信您已经对镜像有所了解,是时候学习容器了. 容器是Docker的另一个核心概念.简单来说,容器是镜像的一个运行实例.正如从虚拟机模板上启动VM一样,用户也同样可以从单个镜像上启 ...

  10. 【翻译】ScyllaDB数据建模的最佳实践

    文章翻译自Scylla官方文档:https://www.scylladb.com/2019/08/20/best-practices-for-data-modeling/ 转载请注明出处:https: ...