Pathfinding in 2D Arrays
Let's say I have this 2D Array map
{ 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,开发者_JAVA技巧0,0,1,1,1,1,1,1,1,1,1 },
and I have HashSet full of Integers that define blocked tiles. 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)? 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 :)
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. The blocked tiles can either be done by inhibiting adjacency (i.e. 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. However, if you take the latter approach, you'll want to ensure that those tiles never inadvertently end up in a solution!
精彩评论