当前位置: 首页 > news >正文

扁平化配色方案网站葫芦岛公司做网站

扁平化配色方案网站,葫芦岛公司做网站,阿里巴巴外贸平台怎么收费,龙华网站建设网站定制A*(A-star)是一种图遍历和寻路算法,由于其完整性、最优性和最佳效率,它被用于计算机科学的许多领域。给定一个加权图、一个源节点和一个目标节点,该算法将找到从源到目标的最短路径(相对于给定的权重&#…

A*(A-star)是一种图遍历和寻路算法,由于其完整性、最优性和最佳效率,它被用于计算机科学的许多领域。给定一个加权图、一个源节点和一个目标节点,该算法将找到从源到目标的最短路径(相对于给定的权重)。

算法过程:遍历方向为从竖直向上沿顺时针方向。

1.首先计算开始节点的G,H,F值,将开始节点放入检测列表中。

2.将检测列表中的所有点按到目标所需的成本的估计值F排序,选择F最小的节点作为当前节点。

3.将当前点从检测列表中移除,加入到已检测列表中。

4.计算当前点周围的八个点G,H,F值,将不包含在检测列表中的点加入到检测列表。

5.重复2,3,4,直到找到目标点。

注:

F = g (n)+ h(n)

gn) 是从起始节点到 n 的路径的成本,hn) 是一个启发式函数,用于估计从 n 到目标的最便宜路径的成本。

代码实现:

改造路径点数据类:

public class DataNode
{public Vector2Int pos;public DataNode parent;//A*使用public int gCost = 999999999;public int hCost;public int fCost;public DataNode(Vector2Int pos, DataNode parent){this.pos = pos;this.parent = parent;}//A*使用计算Fpublic void CalculateFCost(){fCost = gCost + hCost;}
}

算法类:

public class AStar : FindPathAlgorithm
{public AStar(int[,] mapData, int xCount, int zCount) : base(mapData, xCount, zCount){}public override List<Vector2Int> FindPath(Vector2Int startPos, Vector2Int goalPos){DataNode dataNode = this.AStarFind(startPos, goalPos);if (dataNode == null){Debug.LogError("寻路有误,请检查参数是否正确");return null;}return Utils.GetPath(dataNode);}DataNode AStarFind(Vector2Int startPos, Vector2Int goalPos){//存储要检测的点List<DataNode> frontier = new List<DataNode>();//存储已经检测的点List<Vector2Int> reached = new List<Vector2Int>();DataNode startNode = new DataNode(startPos,null);startNode.gCost = 0;startNode.hCost = CalculateDistanceCost(startPos, goalPos);startNode.CalculateFCost();frontier.Add(startNode);while (frontier.Count > 0){DataNode currentNode = GetLowestFCostNode(frontier);if (currentNode.pos == goalPos){return new DataNode(goalPos, currentNode.parent);}frontier.Remove(currentNode);reached.Add(currentNode.pos);List<DataNode> neighbors = GetNeighbors(currentNode.pos, reached);foreach (DataNode neighbourNode in neighbors){int tentativeGCost = currentNode.gCost + CalculateDistanceCost(currentNode.pos, neighbourNode.pos);if (tentativeGCost < neighbourNode.gCost){neighbourNode.parent = currentNode;neighbourNode.gCost = tentativeGCost;neighbourNode.hCost = CalculateDistanceCost(neighbourNode.pos, goalPos);neighbourNode.CalculateFCost();if (!frontier.Contains(neighbourNode)){frontier.Add(neighbourNode);}}}}return null;}List<DataNode> GetNeighbors(Vector2Int current, List<Vector2Int> reached){List<DataNode> neighbors = new List<DataNode>();for (int i = 0; i < Utils.pointDir.Count; i++){Vector2Int neighbor = current + Utils.pointDir[i];if (this.IsCanAdd(neighbor, reached)){neighbors.Add(new DataNode(neighbor,null));}}return neighbors;}bool IsCanAdd(Vector2Int current, List<Vector2Int> reached){if (reached.Contains(current))return false;if (current.x >= 0 && current.y >= 0 && current.x < xCount && current.y < zCount){//如果是障碍物,则不能被Addif (this.mapData[current.y, current.x] == 1){return false;}return true;}return false;}private int CalculateDistanceCost(Vector2Int a, Vector2Int b){return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y);}private DataNode GetLowestFCostNode(List<DataNode> pathNodeList){DataNode lowestFCostNode = pathNodeList[0];for (int i = 1; i < pathNodeList.Count; i++){if (pathNodeList[i].fCost < lowestFCostNode.fCost){lowestFCostNode = pathNodeList[i];}}return lowestFCostNode;}
}

结果:

参考链接:

A* 算法简介 (redblobgames.com)

A* 搜索算法 - 维基百科,自由的百科全书 (wikipedia.org)

A* Pathfinding (E01: algorithm explanation) - YouTube

http://www.15wanjia.com/news/194569.html

相关文章:

  • 如何做一个虚拟网站wordpress 禁用feed
  • 唱片公司网站模板重庆综合网站建设配件
  • 网站开发计划表网站模板搭建
  • 今天的新闻内容摘抄30字网站搜索引擎优化方案范文
  • 兰州网站制作公司在哪里手机模板网站制作
  • 申请网站主机邢台做wap网站找谁
  • 建个网站有收龙之网官网
  • 网站自助搭建平台企业策划书是什么
  • 建设工程信息查询哪个网站好广州物流最新情况
  • 杭州设计 公司 网站建设东莞市区最繁华的地方
  • 网站域名的用处怎么修改网站上的内容
  • 做网站什么东西需要费用网站备案 备注
  • 台州有哪些做网站的公司网站服务建设
  • 志迅东莞网站建设织梦 网站根目录
  • 沈阳网站开发建一个c2c网站要多少钱
  • 网站开发招标任务书上海网站seo诊断
  • 手机网站 制作网络营销推广的主要形式为
  • 站外seo推广旧房改造找哪家
  • 哪里可以下企业网站模板龙岗同乐社区网站建设
  • 网站视觉规范计算机网页制作素材
  • 花店网站建设需求wordpress文件插件
  • 重庆住房与城乡建设部网站怎么重装wordpress
  • 龙岗区建设规划网站湛江百度网站快速排名
  • 网站设计 工作网站开发一般会使用框架吗
  • 做任务赚取佣金网站怎样做模具钢网站
  • 爱站网长尾挖掘工具惠州市建设规划局网站
  • 买网站账号做推广网站设计建设案例
  • 大连哪家公司做网站哪些网站做家政推广
  • 建设网站要用到什么语言广州有什么好玩的
  • 网站如何查看浏览量wordpress有赞收款