C# A*算法的2坐标启发式求解
对于我的学校项目,我用*算法制作了一张二维平铺地图,以找到穿越障碍物的最短路径。我使用了一个公式来获得下一个瓷砖的启发式分数 这是获取启发的函数C# A*算法的2坐标启发式求解,c#,path-finding,C#,Path Finding,对于我的学校项目,我用*算法制作了一张二维平铺地图,以找到穿越障碍物的最短路径。我使用了一个公式来获得下一个瓷砖的启发式分数 这是获取启发的函数 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
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;
}
publicstaticintgeth(intcx,intcy,intex,intey)
{
//cx=当前位置x
//cy=当前位置y
//ex=终点(目标)位置x
//ey=终点(目标)位置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)//如果h为负值,则将其设为正值
{
h=h*-1;
}
返回h;
}
当开始点在y轴上高于结束点时,此方法有效,但当开始点在y轴上低于结束点时,无法找到最有效的路径
我已经添加了问题的控制台版本。最有效的应该是对角线上升,但它走错了路
(蓝色“C”表示已检查节点,绿色“p”表示路径,红色“N”表示仍需检查,其他节点尚未到达)
在链接的文章中,我看到了以下算法: 差异周围的管道符号是,这在C中表示为
Math.Abs
距离没有附加标志<当ex
大于cx
(同样地cy-ey
)时,code>cx-ex将为负值,这可能会导致您的Math.Min
和Math.Max
为d_Min
选择错误的维度。您应该使用Math.Abs
删除所比较数量的符号
e、 g
(同样对于
d_max
)在链接文章中,我看到了以下算法:
差异周围的管道符号是,这在C中表示为Math.Abs
距离没有附加标志<当ex
大于cx
(同样地cy-ey
)时,code>cx-ex将为负值,这可能会导致您的Math.Min
和Math.Max
为d_Min
选择错误的维度。您应该使用Math.Abs
删除所比较数量的符号
e、 g
(同样对于
d_max
)谢谢您的帮助:)谢谢您的帮助:)
int d_min = Math.Min(Math.Abs(cx - ex), Math.Abs(cy - ey));