计算/显示机器人(位置)在2D网格(阵列)上移动的最小成本
我目前正在研究一个计算机器人移动成本(位置 x,y)的作业问题。
我们给出了带有多个障碍物(符号 ##
)的 2D 网格(2 维数组)的尺寸。下面是一个示例网格。我们还给出了机器人的起始位置。在当前位置,它的“成本”为00
(静止)。
<代码>...................................................... ... …………………………………… …………………………………… …………………………………… ..########################################.. …………………………………… …………………………………… …………………………………… ....################..........00......................................... ......################......................................................... ......################......................................................... ......################......................................................... …………………………………… ...................................................... ......................................................
作业中需要计算每个未知网格的成本 (..
),以便显示机器人移动到该位置所需的成本。
水平移动+2 前一个网格的成本。
对角线移动前一个网格的成本+3。
机器人不能越过障碍物,必须绕过它。
每个值必须具有最小成本(例如:对角线移动的成本比水平和垂直移动的成本少)。
下面是我们应该得到的结果(仅显示成本的最后两位数字,省略了一些值,因此更具可读性):
https://i.sstatic.net/JiDl1.png
现在我无法想象/解决这个问题。我们被告知,它“在道德上就像冒泡排序算法”,每次找到新的最小成本时,所有内容都会重新计算。
抱歉,如果这非常令人困惑,任何建议(代码或伪代码将非常受欢迎)
I am currently working on an assignment question that calculates the cost of movement for a robot (position x, y).
We are given the dimensions of a 2D grid (2 dimension array) with several obstacles (symbol ##
). Below is a sample grid. We're also given the starting position of the robot. At the current position it has a 'cost' of 00
(stationary).
..................................................
........................................##........
........................................##........
........................................##........
..........######################################..
........................................##........
........................................##........
........................................##........
....################..........00........##........
....################....................##........
....################....................##........
....################....................##........
........................................##........
..................................................
..................................................
What is required in the assignment is to calculate the cost of each grid that is unknown (..
) so it shows the cost the robot has to make in order to travel to that position.
Horizontal movements +2 to the cost of the previous grid.
Diagonal movements +3 to the cost of the previous grid.
The robot can't cross and obstacle and must go around it.
Each value must have the minimum cost (eg: less cost to travel diagonally than horizontally and vertically).
Below is the result we should have (only displaying the last two digits of cost with some values omitted so it is more readable):
https://i.sstatic.net/JiDl1.png
Right now I'm having trouble visualizing/tackling the problem. We've been told that it is 'morally like the bubble sort algorithm' where each time there is a new minimum cost is found, everything is recalculated.
Apologies if this is terribly confusing, any suggestions (code or pseudocode would be greatly welcome)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恕我直言,可视化路径问题的最简单方法是作为一个连接的节点网络(图),其中邻居节点(机器人可以从当前位置移动到的方块)通过加权边相互连接(权重是移动成本)节点之间)。
N = 节点,数字和线是带有权重的边,@ 是障碍
Djikstra 算法 是已经建议,有关更多选项,请参阅例如 http://en.wikipedia.org/wiki/Shortest_path_problem。 二进制最小堆可以很好地用作优先级队列。 (据我所知,由于速度的原因,A* + 二进制最小堆在实时游戏中被大量使用)。
编辑:我之前建议使用 A*,但仔细想想,它不适用于单源最短路径 - 问题那么好。
Imho, the easiest way to visualize path-problems is as a connected network of nodes (graph), where the neighbour nodes (squares the robot can move into from current position) are connected to each other with weighed edges (weight being the movement cost between the nodes).
N = node, numbers and lines are edges with weights, @ is an obstacle
Djikstra's algorithm was already suggested, for even more options see for example http://en.wikipedia.org/wiki/Shortest_path_problem. Binary min heap works good as the priority queue. (afaik, A* + binary min heap is used in realtime games a lot because of the speed).
Edit: I suggested A* earlier, but come to think of it, it wouldn't work for single-source shortest paths -problem that good.