我正在审查这个堆栈溢出帖子 Python - Speed up an Star Pathfinding Algorithm
我试图确定这条线for tile in graph[current]:
代表什么。即graph[]
代表什么。我觉得 graph 应该代表整个网格,但我第二次猜测这是因为我们将 current 作为参数提供给 graph 上的 [] 运算符,所以它必须返回一些东西,但我不确定它应该是什么。也许我们可以前往的瓷砖直接与当前相邻?
还有这个语法是什么意思current = heapq.heappop(openHeap)[1]
?
import heapq
def aStar(self, graph, current, end):
openSet = set()
openHeap = []
closedSet = set()
def retracePath(c):
path = [c]
while c.parent is not None:
c = c.parent
path.append(c)
path.reverse()
return path
openSet.add(current)
openHeap.append((0,current))
while openSet:
current = heapq.heappop(openHeap)[1]
if current == end:
return retracePath(current)
openSet.remove(current)
closedSet.add(current)
for tile in graph[current]:
if tile not in closedSet:
tile.H = (abs(end.x-tile.x)+abs(end.y-tile.y))*10
if tile not in openSet:
openSet.add(tile)
heapq.heappush(openHeap, (tile.H,tile))
tile.parent = current
return []