a* 室内路径最优解 python实现

import math

启发间隔, 其时点和目标点的启发间隔; — 就是简略的曼哈顿间隔

def heuristic_distace(Neighbour_node,target_node):
H = abs(Neighbour_node[0] – target_appearnode[0]) + abs(Neighbour_node[1] – target_龚俊node[1])
return H

def go_around(direction):
box_lengtapp是什么意思h = 1
diagonal_lin狗狗币e = box_lengtGohappointment * 1
if (direction==0 or directionapple==2 or direction==6 or direction==8):
return diagonal_line
elif (direction==1 or direction==3 or direction==4 or direcGotion==5 or direction==7):
return diagonal_line

def find_coordinate(map,symble):
#store coordina宫颈癌早期症状te
result=[]

for index1,value1Go in enumerate(map):
if symble in value1:
row = indAPPex1
for index2, value2 in enumerate(map[index1]):
if symble==value2:
cgoogleolumn = index2
result.append([row, column])
return result

def show_map(map):
for idx in map:
print(idx)

map =[[“.”, “.”, “.”, “#”, “.”, “#”, “.”, “.”, “.”, “.”],
[“.”, “.”, “#”, “.”,APP “.”, “#”, “.”, “#”, “.”, “#”],
[“s”, “.”, “#”, “.”, “#”, “.”, “#”, “.”, “.”, “.”],
[“.”, “#”,apple “#”, “.”, “.”, “.”, “.”, “.”, “#”, “.”],
[“.”, “.”, “.”, “.”, “#”, “#”, “.”, “.”, “#”, “.”],
[“.”, “#”, “.”, “.”, “.”, “.”, “#”, “.”, “.”, “.”],
[“.”, “#”, “.”, “.”, “.”, “#”, “#”工程造价, “.”, “#”, “.”],
[“.”, “.”, “.”, “.”, “.”, “.”, “.”, “.”, “#”, “.”],
[“.”, “#”, “#”, “.”, “.”, “.”, “#”, “.”, “.”, “.”],
[“.”, “.”, “.”, “#application“, “宫颈癌早期症状#”, “#”, “.”, “工程造价.”, “#”Go, “f”],
[“#”, “#”, “.”,app下载 “.”, “#”, “#工商银行“, “#”, “.”, “#”, “.”],
[“.”, “#”, “#”app是什么意思, “.”, “.”, “.”, “#”, “.”, “.”, “.”],
[“.”, “.”, “.”, “.”, “#”APP, “#app下载“, “.”, “公积金.”, “#”, “.”]]

#these datas are store in tgooglehe form of list in a singal list

记载一切的障碍物点 坐标

obstacle = find_coordinate(map,”#”)
start_node = find_coorappeardinate(map,”s”)[0]
target_node = find_coordinate(map,”f”)[0]

curr狗狗币ent_node = start_node

设置路径起点为其时节点

path_appearvertices = [stappleart_node]

#v工程造价isited_vertices should be stored in the form of a singal list
Neighbour_vertices = []

进入查找

while current_node != target_node:

x_coordinate = currentAPP_node[0]
y_coordinate = current_node[1]
F = []  # 节点权值 F = gapple + h
Neighbour_vertices =   [[x_coordinateappointment - 1, y_coordinate - 1],
[appearx_coordinate - 1, y_coordinate    ],
[x_coordinate -appstore 1, y_coordinate + 1],
[x_coordinate,     y_coordinate - 1],
[x_coo宫颈癌早期症状rdinate    , y_coordinate    ],
[x_coordinaapp是什么意思te,     y_coordinate + 1],
[x_coordinate + 1, y_coordinate - 1],
[x_coordinate + 1, y_coordinate    ],
[x_coordinate + 1, y_coordinate + 1]]
# 遍历相邻坐标
for index, vaappointmentlue in enumerate(Neighbour_vertices):
if value[0] in range(len(map)):
if value[1] in range(len(map)):
if value not in obstacle+path_vertices:
# 假如满意节点 1, 在地图边界内 2, 不在障碍物点和已经通过的点, 核算权重
F.append(heuristic_distace(value, target_node) + go_around(index))
map[value[0]][value[1]] = str(F[-1])app是什么意思
else:
F.append(10000)
else:
F.append(10000)
else:
F.append(10000)
#a very large number
# print(F) # 打印出遍历的 节点的权重
#将公积金其时点设置为 权重最小的点
current_node=Neighbour_verticeapp是什么意思s[F.index(min(total_distance for tot公积金al_distance in F))]
map[current_node[0]]狗狗币[cuapp下载rrent_node[1]] = str(min(F))
show_map(map宫颈癌)
print(len(path_vappointmentertices))
path_vertices.append(c宫颈癌urrent_nod公积金e)
# if current_node not in visited_vertices:
#     visited_vertices.app工商银行end(current_node)
# elsapplicatione:
#     print("the枸杞re is no route between")
#     break

print(path_v公积金ertices)

评论

发表回复