algorithm - A* 寻路——我想我已经用伪代码记下了,需要验证

标签 algorithm pseudocode path-finding a-star

他们说,把别人说的话拿过来,然后重复一遍是验证概念基础(理解)的最佳形式。所以,我一直在阅读有关 A* 的内容并将其翻译成伪代码,我想我明白了。任何人都可以验证和/或提供有关此实现是否有效的提示吗?

对不起,如果这很匆忙,我正在休息;)

openList.ClearTiles
closeList.clearTiles
path.clearTiles

openList.Add startTile

While openList.Count > 0 and PathFound = false
    activeTile = openList.GetTileWithLowestPathCost
    openList.remove activeTile
    closeList.add activeTile

    if targetTile.equals(activeTile)
       pathFound = true
    else
       for each activeTile.neighbors as nTile
           if nTile not in openList and not in closeList and IsMovable
              nTile.parent = activeTile
              nTile.hueristic = computeHeuristic
              nTile.movementCost = computeMovementCost
              nTile.pathCost = nTile.hueristic + nTile.movementCost

              openList.add nTile
           elseif isMovable = false
              closelist.add nTile
           endif
       next
    endif
endwhile

if pathFound = true        
   while activeTile.parent is not nothing
       path.insertAtZero activeTile
       activeTile = activeTile.parent
   endwhile
endif

最佳答案

你的伪代码有一个主要问题和一个小问题。

主要问题:
当您找到nTile 时,它可能处于closed 状态或open 状态,在您的实现中- 您忽略它并跳过它。但是,如果它处于关闭或打开状态,您应该检查您刚刚发现的路径成本是否更好然后是关闭打开,如果是,则将其从 closed/open 中移除,并使用新的路径成本将其重新插入到 open

小问题:
有时目标节点不止一个[有一组目标节点,你正在寻找到其中一个的路径]。因此,您可以检查 if targetTile.equals(activeTile) 而不是 if heuristicValue(activeTile) == 0 [假设启发式函数是 admissible] 或检查 activeTile 是否在 targetStates 中

关于algorithm - A* 寻路——我想我已经用伪代码记下了,需要验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320176/

相关文章:

java - 在 SD 卡上组织数据以便快速搜索的最佳方式

python - 为什么即使没有重复计算,memoization 也可以加速 Python 中的阶乘?

algorithm - 如何检查一个数字是否是回文?

java - 如何从二维数组中的点开始增加相邻单元格

algorithm - 具有强制运动约束的 Dijkstra

Java - gov.nasa.jpf.jvm.Verify Pathfinder 包不存在

algorithm - 这个游戏背后的数学/计算原理是什么?

c - 使用 openMP 任务时出现意外行为

algorithm - D* 精简版 : how to compare and sort that paired keys?

java - 在不使用线程的情况下实现此寻路代码的好方法是什么?