This commit is contained in:
weixin_46229132 2025-05-04 21:32:13 +08:00
parent b626f667d9
commit 1833258f34

View File

@ -1,15 +1,5 @@
import heapq
# 停车场状态矩阵
parking_lot = [
[3, 2, 2, 0, 2, 2, 2],
[3, 1, 1, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0]
]
# 起点和终点(行, 列)
start = (0, 3)
goal = (1, 1)
# 可行走的位置即值为0或目标位置
def is_walkable(pos):
@ -18,10 +8,12 @@ def is_walkable(pos):
return parking_lot[r][c] == 0 or pos == goal
return False
# 曼哈顿距离作为启发函数
def heuristic(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
# A*搜索
def a_star(start, goal):
open_set = []
@ -56,6 +48,18 @@ def a_star(start, goal):
return None # 找不到路径
if __name__ == "__main__":
# 停车场状态矩阵
parking_lot = [
[3, 2, 2, 0, 2, 2, 2],
[3, 1, 1, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0]
]
# 起点和终点(行, 列)
start = (0, 3)
goal = (1, 1)
# 执行 A* 路径搜索
path = a_star(start, goal)