【发布时间】:2016-10-10 12:05:53
【问题描述】:
对于我的学校项目,我使用 A* 算法制作了一个 2D 瓦片地图,以找到穿过障碍物的最短路径。我使用公式从http://www.growingwiththeweb.com/2012/06/a-pathfinding-algorithm.html 获得下一个图块的启发式分数
这是获取启发式的函数
public static int geth(int cx, int cy, int ex, int ey)
{
//cx = current position x
//cy = current position y
//ex = end (goal) position x
//ey = end (goal) position y
int c = 14;
int d_min = Math.Min(cx - ex, cy - ey);
int d_max = Math.Max(cx - ex, cy - ey);
int h = c * d_min + (d_max - d_min);
if (h < 0) //make h positive in case it's negative
{
h = h * -1;
}
return h;
}
这在 y 轴上的起点高于终点时有效,但在 y 轴上的起点较低时找不到最有效的路径。
我添加了我的问题的控制台版本。最高效的应该是斜向上,但走错了路。
(蓝色'C'是检查的节点,绿色'P'路径已建立,红色'N'仍有待检查,其他尚未到达)
【问题讨论】:
标签: c# path-finding