你将看到一只昆虫从原点(0,0)开始在平面上移动。只能向东北、东南、西北和西南移动的昆虫。现在的问题是,昆虫在一个方向上一次只能移动特定数量的步。例如,它只能在东北方向移动3步,在东南方向移动4步,在西北方向移动5步,在西南方向移动2步。你知道吗
如果昆虫在一段时间内向NE方向移动了3步-它必须向NW、SE或SW方向移动-然后再向NE方向移动。你知道吗
在这种情况下,如何获得到达给定点的最有效路径? 我是编程新手,遇到了这个问题。有谁能告诉我解决这类问题的好方法吗?非常感谢。你知道吗
我的想法:
首先,在东北和东南各走一步,在东边走两步。同样,在北边、南边和西边走两步。现在,找到最短路径。基本上,把这个问题变成一个简单的问题,而不是走对角线。(不是个好方法。但是你可以找到一条路。)
第二种方法的灵感来自于用于寻找三维曲线极小值的迭代方法。如果我取消了一次只能朝一个方向行驶的限制,我们可以朝着(四个方向中的一个方向)行驶,这样我们可以更快地到达目的地。这类似于梯度下降法,但只有四个方向移动。你知道吗
欢迎使用Python代码。
TL;DR:最短路径=>;考虑Dijkstra算法。你知道吗
在这里您可以想到一个节点为
(Last Move, Position)
的图。 计算四个可能的Last Move
的最短路径并取最短路径。你知道吗注意:如果你只关心移动的次数,而不关心移动的距离,那么广度优先搜索更有效。你知道吗
以下是BFS案例的一些代码:
如果目标真的很远,你可以使用通常的改进方法:双向搜索,A*算法和一些启发式算法,等等
相关问题 更多 >
编程相关推荐