0: # 在open_list中查找F值最小的节点作为当前方格节点 cu.._a星算法python">
当前位置:   article > 正文

python A星寻路算法_a星算法python

a星算法python
  1. def a_star_search(start,end):
  2. # 待访问的格子
  3. open_list = []
  4. # 已访问的格子
  5. close_list = []
  6. # 把起点加入open_list
  7. open_list.append(start)
  8. """主循环,每一轮检查一个当前方格节点"""
  9. while len(open_list) > 0:
  10. # 在open_list中查找F值最小的节点作为当前方格节点
  11. current_grid = find_min_grid(open_list)
  12. # 当前方格节点从openList中移除
  13. open_list.remove(current_grid)
  14. # 当前方格节点进入 closeList
  15. close_list.append(current_grid)
  16. # 找到所有邻近节点
  17. neighbors = find_neighbors(current_grid,open_list,close_list)
  18. for grid in neighbors:
  19. # 邻近节点不在openList中,标记父亲、G、H、F,并放入openList
  20. grid.init_grid(current_grid,end)
  21. open_list.append(grid)
  22. # 如果终点在openList中,直接返回终点格子
  23. for grid in open_list:
  24. if (grid.x == end.x) and (grid.y == end.y):
  25. return grid
  26. """openList用尽,仍然找不到终点,说明终点不可到达,返回空"""
  27. return None
  28. def find_min_grid(open_list=[]):
  29. temp_grid = open_list[0]
  30. for grid in open_list:
  31. if grid.f < temp_grid.f:
  32. temp_grid = grid
  33. return temp_grid
  34. def find_neighbors(grid,open_list=[],close_list=[]):
  35. grid_list = []
  36. if is_valid_grid(grid.x,grid.y-1,open_list,close_list):
  37. grid_list.append(Grid(grid.x,grid.y-1))
  38. if is_valid_grid(grid.x, grid.y+1, open_list, close_list):
  39. grid_list.append(Grid(grid.x, grid.y+1))
  40. if is_valid_grid(grid.x-1, grid.y, open_list, close_list):
  41. grid_list.append(Grid(grid.x-1, grid.y))
  42. if is_valid_grid(grid.x+1, grid.y, open_list, close_list):
  43. grid_list.append(Grid(grid.x+1, grid.y))
  44. return grid_list
  45. def is_valid_grid(x,y,open_list=[],close_list=[]):
  46. # 是否超过边界
  47. if x<0 or x>=len(MAZE) or y<0 or y>=len(MAZE[0]):
  48. return False
  49. # 是否有障碍物
  50. if MAZE[x][y] == 1:
  51. return False
  52. # 是否在open_list中
  53. if contain_grid(open_list,x,y):
  54. return False
  55. # 是否在closeList中
  56. if contain_grid(close_list,x,y):
  57. return False
  58. return True
  59. def contain_grid(grids,x,y):
  60. for grid in grids:
  61. if (grid.x==x) and (grid.y==y):
  62. return True
  63. return False
  64. class Grid:
  65. def __init__(self,x,y):
  66. self.x = x
  67. self.y = y
  68. self.f = 0
  69. self.g = 0
  70. self.h = 0
  71. self.parent = None
  72. def init_grid(self,parent,end):
  73. self.parent = parent
  74. self.g = parent.g + 1
  75. self.h = abs(self.x - end.x) + abs(self.y - end.y)
  76. self.f = self.g + self.h
  77. if __name__ == '__main__':
  78. # 迷宫地图
  79. MAZE = [
  80. [0, 0, 0, 0, 0, 0, 0],
  81. [0, 0, 0, 1, 0, 0, 0],
  82. [0, 0, 0, 1, 0, 0, 0],
  83. [0, 0, 0, 1, 0, 0, 0],
  84. [0, 0, 0, 0, 0, 0, 0]
  85. ]
  86. # 设置起点和终点
  87. start_grid = Grid(2,1)
  88. end_grid = Grid(2,5)
  89. # 搜索迷宫终点
  90. result_grid = a_star_search(start_grid,end_grid)
  91. # 回溯迷宫路径
  92. path = []
  93. while result_grid is not None:
  94. path.append(Grid(result_grid.x,result_grid.y))
  95. result_grid = result_grid.parent
  96. # 输出迷宫和路径,路径用星号表示
  97. for i in range(0,len(MAZE)):
  98. for j in range(0,len(MAZE[0])):
  99. if contain_grid(path,i,j):
  100. print("*, ",end='')
  101. else:
  102. print(str(MAZE[i][j])+', ',end='')
  103. print()

输出:

  1. 0, 0, *, *, *, *, 0,
  2. 0, 0, *, 1, 0, *, 0,
  3. 0, *, *, 1, 0, *, 0,
  4. 0, 0, 0, 1, 0, 0, 0,
  5. 0, 0, 0, 0, 0, 0, 0,

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/561097
推荐阅读
相关标签
  

闽ICP备14008679号