九五美食网
您的当前位置:首页深入理解js A*寻路算法原理与具体实现过程

深入理解js A*寻路算法原理与具体实现过程

来源:九五美食网


如果开启列表已经空了, 说明路径不存在.

最后从目标格开始, 沿着每一格的父节点移动直到回到起始格, 这就是路径.

主要代码

程序中的 "开启列表" 和 "关闭列表"

List<Point> CloseList;
List<Point> OpenList;

Point 类

public class Point
{
 public Point ParentPoint { get; set; }
 public int F { get; set; } //F=G+H
 public int G { get; set; }
 public int H { get; set; }
 public int X { get; set; }
 public int Y { get; set; }
 public Point(int x, int y)
 {
 this.X = x;
 this.Y = y;
 }
 public void CalcF()
 {
 this.F = this.G + this.H;
 }
}

寻路过程

public Point FindPath(Point start, Point end, bool IsIgnoreCorner)
{
 OpenList.Add(start);
 while (OpenList.Count != 0)
 {
 //找出F值最小的点
 var tempStart = OpenList.MinPoint();
 OpenList.RemoveAt(0);
 CloseList.Add(tempStart);
 //找出它相邻的点
 var surroundPoints = SurrroundPoints(tempStart, IsIgnoreCorner);
 foreach (Point point in surroundPoints)
 {
 if (OpenList.Exists(point))
 //计算G值, 如果比原来的大, 就什么都不做, 否则设置它的父节点为当前点,并更新G和F
 FoundPoint(tempStart, point);
 else
 //如果它们不在开始列表里, 就加入, 并设置父节点,并计算GHF
 NotFoundPoint(tempStart, end, point);
 }
 if (OpenList.Get(end) != null)
 return OpenList.Get(end);
 }
 return OpenList.Get(end);
}

完整实例代码点击此处本站下载。

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数算用法总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript数组操作技巧总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》及《JavaScript错误与调试技巧总结》

希望本文所述对大家JavaScript程序设计有所帮助。

显示全文