我正在尝试在我正在制作的游戏中添加A *路径查找。 我现在知道怎么做,但我不明白回报是做什么的。 http://en.wikipedia.org/wiki/A*_search_algorithm是我正在使用的参考。 问题:
return reconstruct_path(came_from, goal)做什么? 何时调用return failure ?I am trying to add A* path finding in a game I'm making. I understand how to do it now but I don't understand what the returns do. http://en.wikipedia.org/wiki/A*_search_algorithm is the reference that I'm using. Questions:
What does return reconstruct_path(came_from, goal) do? When is return failure invoked?最满意答案
这是您的维基百科链接中的功能:
function reconstruct_path(came_from, current_node) if current_node in came_from p := reconstruct_path(came_from, came_from[current_node]) return (p + current_node) else return current_nodereturn reconstruct_path(came_from, goal)调用此函数。 第一个if递归调用reconstruct_path ,每次使用不同的current_node ,直到current_node不在came_from (直到if失败)。 每次调用自身时,它返回p := p + current_node 。 p不断添加到每个调用,使p成为一个节点链。
它正在遍历的东西来自于某个地图。 它可能看起来像这样:
1, 2 2, 3 3, 4调用came_from[1]会返回2.或者,你可以说,“2来自1”。
简而言之: reconstruct_path自己调用,每次在地图中came_from走一步,直到它构建路径p 。 这可能看起来像“1,2,3,4”,坚持上面的例子。
无法找到路径时调用return failure 。
Here's the function from your Wikipedia link:
function reconstruct_path(came_from, current_node) if current_node in came_from p := reconstruct_path(came_from, came_from[current_node]) return (p + current_node) else return current_nodereturn reconstruct_path(came_from, goal) calls this function. The first if recursively calls reconstruct_path, each time with a different current_node, until the current_node is not in came_from (until the if fails). Each time it calls itself, it returns p := p + current_node. p keeps getting added to with each call, making p into a chain of nodes.
The thing that it's traversing, came_from, is some map. It might look something like this:
1, 2 2, 3 3, 4Calling came_from[1] would return 2. Or, you could say, "2 came from 1".
In short: reconstruct_path calls itself, each time walking a step back in the map came_from, until it's build a path p. This might look like, "1, 2, 3, 4", sticking with the example above.
return failure is called when a path cannot be found.
更多推荐
发布评论