ios - A* 仅在某些情况下有效

标签 ios objective-c path-finding shortest-path a-star

我的 a* 路径查找算法仅适用于某些情况,但我不明白为什么。我的网格中的每个节点都是可步行的,因此理论上每条路径都应该有效。我相信错误在这一行:

 PathFindingNode *neighbor = NULL;
            if ((y > 0 && x > 0) && (y < gridY - 1 && x < gridX - 1))
             neighbor = [[grid objectAtIndex:x + dx] objectAtIndex:y +dy];

最佳答案

在函数 -(void)addNeighbors: 中,该行

if ((y > 0 && x > 0) && (y < gridY - 1 && x < gridX - 1))
    neighbor = [[grid objectAtIndex:x + dx] objectAtIndex:y +dy];

有错误,因为如果 curNode 位于边界上,它不会将邻居添加到队列中。这样算法永远不会到达四个角的endNode(即[0,0], [gridX-1,0], [0,gridY-1], [gridX-1,gridY- 1])。

关于ios - A* 仅在某些情况下有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20834482/

相关文章:

java - ActiveMQ Broker Factory 找不到用于故障转移的工厂类

ios - 如何指定弹出 Controller 的箭头指向的位置

objective-c - SKStoreProductViewController 给出 Domain=SKErrorDomain Code=5 iOS7 错误

ios - UIDatePickerModeTime 限制时间选择或设置时间范围

iphone - 确定 UILocalNotification 是在前台还是后台通过应用程序触发的

ios - 使用 SKNode 的寻路不工作

java - 不走视觉理想路线的星型算法

ios - 将水平和垂直 CAGradientLayer Mask 应用于 UIScrollView/UICollectionView

ios - LAContext评估策略并不总是提示用户

ios - 使用导航 Controller 作为开始屏幕 ios