我正在尝试GeeksforGeeks 的 A* 大纲。我遵循了灰色框中的大部分步骤,直到我在 dii 和 diii 上遇到了障碍。这是寻路部分:
def pathfind(grid):
sx, sy = 0, 0
# find start point and end point cood
for y in range(len(grid)):
for x in range(len(grid[y])):
if grid[y][x] == "S":
sx = x
sy = y
elif grid[y][x] == "E":
ex = x
ey = y
opensq = []
closedsq = []
successor = []
#add starting point to closed
opensq.append([sx, sy, gcost(sx, sy, sx, sy), hcost(sx, sy, ex, ey)])
grid[sy][sx] = "O"
while opensq:
# find the node with lowest fcost
q = opensq[0]
if len(opensq) == 1:
pass
else:
for sq in range(len(opensq)):
if sq == len(opensq) - 1:
pass
else:
if q[2] + q[3] < opensq[sq + 1][2] + opensq[sq + 1][3]:
pass
elif q[2] + q[3] == opensq[sq + 1][2] + opensq[sq + 1][3]:
# if f is same, check hcost
if q[3] < opensq[sq + 1][3]:
pass
elif q[3] == opensq[sq + 1][3]:
pass
else:
q = opensq[sq + 1]
else:
q = opensq[sq + 1]
opensq.pop(opensq.index(q))
# pick successors to q
successors = []
successors.append([q[0] + 1, q[1], gcost(q[0] + 1, q[1], sx, sy), hcost(q[0] + 1, q[1], ex, ey)])
successors.append([q[0] - 1, q[1], gcost(q[0] - 1, q[1], sx, sy), hcost(q[0] - 1, q[1], ex, ey)])
successors.append([q[0], q[1] + 1, gcost(q[0], q[1] + 1, sx, sy), hcost(q[0], q[1] + 1, ex, ey)])
successors.append([q[0], q[1] - 1, gcost(q[0], q[1] - 1, sx, sy), hcost(q[0], q[1] - 1, ex, ey)])
aluckdog
相关分类