我有一个程序,我想找到一个目标之间的距离,然后一个接一个地到终点。它可以找到第一个目标,但找不到第二个目标。
代码找到了目标,但随后开始在随机方向上搜索,就像 dijkstra 的那样。如果它访问结束节点,它会简单地覆盖它,就好像它是一个空节点一样。它也不会访问之前在算法的第一步中检查过的任何节点。任何帮助将不胜感激!
这里的算法代码:
def A_Star_algorithm(draw, grid, start, end, target_check, target):
if not target:
count = 0
open_set = PriorityQueue()
open_set.put((0, count, start))
came_from = {}
g_score = {node: float("inf") for row in grid for node in row}
g_score[start] = 0
f_score = {node: float("inf") for row in grid for node in row}
f_score[start] = h(start.get_pos(), end.get_pos())
open_set_hash = {start}
while not open_set.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = open_set.get()[2]
open_set_hash.remove(current)
if current == end:
reconstruct_path(came_from, end, draw, start)
end.make_end()
return True
for neighbor in current.neighbors:
temp_g_score = g_score[current] + 1
if temp_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = temp_g_score
f_score[neighbor] = temp_g_score + h(neighbor.get_pos(), end.get_pos())
if neighbor not in open_set_hash:
count += 1
open_set.put((f_score[neighbor], count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
draw()
if current != start:
current.make_closed()
elif target:
count = 0
open_set = PriorityQueue()
open_set.put((0, count, start))
came_from = {}
g_score = {node: float("inf") for row in grid for node in row}
g_score[start] = 0
f_score = {node: float("inf") for row in grid for node in row}
f_score[start] = h(start.get_pos(), target.get_pos())
open_set_hash = {start}
while not open_set.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = open_set.get()[2]
open_set_hash.remove(current)
if current == target:
reconstruct_path(came_from, target, draw, start)
target.make_target()
open_set.queue.clear()
for neighbor in current.neighbors:
temp_g_score = g_score[current] + 1
if temp_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = temp_g_score
f_score[neighbor] = temp_g_score + h(neighbor.get_pos(), target.get_pos())
if neighbor not in open_set_hash:
count += 1
open_set.put((f_score[neighbor], count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
draw()
if current != start:
current.make_closed()
count = 0
open_set = PriorityQueue()
open_set.put((0, count, target))
came_from = {}
g_score = {node: float("inf") for row in grid for node in row}
g_score[target] = 0
f_score = {node: float("inf") for row in grid for node in row}
f_score[target] = h(target.get_pos(), end.get_pos())
open_set_hash.clear()
open_set_hash = {target}
while not open_set.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = open_set.get()[2]
open_set_hash.remove(current2)
if current == end:
reconstruct_path(came_from, end, draw, target)
end.make_end()
return True
for neighbor in current.neighbors:
temp_g_score = g_score[current2] + 1
if temp_g_score < g_score[neighbor]:
came_from[neighbor] = current2
g_score[neighbor] = temp_g_score
f_score[neighbor] = temp_g_score + h(neighbor.get_pos(), end.get_pos())
if neighbor not in open_set_hash:
count += 1
open_set.put((f_score[neighbor], count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
draw()
if current != target:
current.make_closed()
return False