請問有人會用networkx的astar_path方法嗎?
G=networkx.grid_graph(dim=[30,30])
print(networkx.astar_path(G,(0,0),(5,5)))
[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]
我想在矩陣中加入障礙物點
但不知道怎麼加
求幫忙
可以使用 remove_node
移除節點,達到放置障礙物的效果。
import networkx as nx
G=nx.grid_graph(dim=[3,3])
print(G.nodes())
# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
G.remove_node((1,0))
G.remove_node((1,2))
print(G.nodes())
# [(0, 0), (0, 1), (0, 2), (1, 1), (2, 0), (2, 1), (2, 2)]
print(nx.astar_path(G,(0,0),(2,2)))
# [(0, 0), (0, 1), (1, 1), (2, 1), (2, 2)]