不明白伪代码中返回的内容(Don't understand what return does in pseudo code)

编程入门 行业动态 更新时间:2024-10-15 16:17:18
不明白伪代码中返回的内容(Don't understand what return does in pseudo code)

我正在尝试在我正在制作的游戏中添加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_node

return 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_node

return 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, 4

Calling 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.

更多推荐

本文发布于:2023-07-27 20:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1294990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不明白   代码   内容   understand   pseudo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!