简体   繁体   English

2D阵列中的寻路

[英]Pathfinding in 2D Arrays

Let's say I have this 2D Array map 假设我有这个2D阵列图

{ 0,0,0,0,7,1,1,1,1,1,1,1,1 },
{ 0,7,7,7,7,1,1,1,24,1,1,1,1 },
{ 0,7,24,24,24,24,24,24,24,1,1,3,1 },
{ 0,7,23,23,23,23,23,23,24,1,1,3,1 },
{ 0,7,24,23,23,23,23,23,23,1,1,1,1 },
{ 0,7,24,23,23,23,23,23,23,1,1,1,1 },
{ 0,7,23,23,23,23,23,23,24,1,3,1,1 },
{ 0,7,24,24,24,24,24,24,24,1,3,1,1 },
{ 0,0,0,0,1,1,1,1,1,1,1,1,1 },

and I have HashSet full of Integers that define blocked tiles. 而且我的HashSet充满了整数,这些整数定义被阻止的图块。 What would be a good way so that when I click on one part of the map from where my player is standing to do a good pathfinding? 当我从玩家站立的位置单击地图的一部分以进行良好的寻路时,有什么好方法? A* (using nodes/etc)? A *(使用节点/等)? What do you suggest? 你有什么建议?

Thanks. 谢谢。

If the size of your graph is actually in the order of the example you've described, then you can safely use Dijkstra's algorithm , given that it's somewhat simpler to implement than A*, and there is no real need for heuristic algorithms if you can do an exhaustive search in almost the same time :) 如果图的大小实际上与您描述的示例的顺序相同,则可以安全地使用Dijkstra的算法 ,因为它的实现比A *简单一些,并且如果可以的话,就不需要真正的启发式算法了。几乎同时进行详尽搜索:)

As for your comment about "using nodes/etc", this already is a graph, albeit a slightly akward representation of one. 至于您对“使用节点/等”的评论,尽管已经有点笨拙,但它已经是一张图表。 Every array value is a node, and "edges" are given by adjacency in the array. 每个数组值都是一个节点,并且“ edges”由数组中的邻接关系给定。 The blocked tiles can either be done by inhibiting adjacency (ie look up the list of blocked tiles to determine whether another node is reachable from the current one under consideration), or as Yossarian suggested above, just set the cost of that tile to something so large as to be practically infinite. 可以通过禁止邻接来完成被阻止的切片(即,查找被阻止的切片的列表,以确定正在考虑的当前节点是否可以到达另一个节点),或者如上文Yossarian所建议的那样,只需将该块的成本设置为某种程度即可几乎无限大。 However, if you take the latter approach, you'll want to ensure that those tiles never inadvertently end up in a solution! 但是,如果采用后一种方法,则将要确保这些图块永远不会无意间出现在解决方案中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM