我有下面的代码,但输出没有给我坐标,只是 [
from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
from pathfinding.core.diagonal_movement import DiagonalMovement
matrix = [
[1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]
]
grid = Grid(matrix= matrix)
start = grid.node(0,0) # start in top left hand corner
end = grid.node(5,2) # end in bottom right hand corner
finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
path, runs = finder.find_path(start, end, grid) # returns two things
print(path)
print(runs) # ran through 17 times to find the shortest path
您应该迭代路径列表并将每个 GridNode 对象转换为其相应的坐标。请参阅下面的代码:
matrix = [
[1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1]
]
grid = Grid(matrix=matrix)
start = grid.node(0, 0) # start in top left hand corner
end = grid.node(5, 2) # end in bottom right hand corner
finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
path, runs = finder.find_path(start, end, grid) # returns two things
# Convert GridNode objects to coordinates and print them
coordinates = [(node.x, node.y) for node in path]
print(coordinates)
# Print the number of runs
print(runs)
输出:
[(0, 0), (1, 0), (2, 0), (3, 0), (4, 1), (5, 2)]
10