A*算法作为快速的寻路算法,可以快速查询两点之间的最短路径,下面是代码

参考网址:https://github.com/xclu/Python-1/blob/master/graphs/a_star.py

# function to search the path
def a_star_search(grid: list, begin_point: list, target_point: list, cost=1):assert ((grid[begin_point[0]][begin_point[1]] != 1) and (grid[target_point[0]][target_point[1]] != 1))# the cost map which pushes the path closer to the goalheuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]for i in range(len(grid)):for j in range(len(grid[0])):heuristic[i][j] = abs(i - target_point[0]) + abs(j - target_point[1])if grid[i][j] == 1:heuristic[i][j] = 99  # added extra penalty in the heuristic map# the actions we can takedelta = [[-1, 0],  # go up[0, -1],  # go left[1, 0],  # go down[0, 1]]  # go rightclose_matrix = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]  # the referrence gridclose_matrix[begin_point[0]][begin_point[1]] = 1action_matrix = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]  # the action gridx = begin_point[0]y = begin_point[1]g = 0f = g + heuristic[begin_point[0]][begin_point[0]]cell = [[f, g, x, y]]found = False  # flag that is set when search is completeresign = False  # flag set if we can't find expandwhile not found and not resign:if len(cell) == 0:resign = Truereturn None, Noneelse:cell.sort()  # to choose the least costliest action so as to move closer to the goalcell.reverse()next = cell.pop()x = next[2]y = next[3]g = next[1]f = next[0]if x == target_point[0] and y == target_point[1]:found = Trueelse:# delta have four stepsfor i in range(len(delta)):  # to try out different valid actionsx2 = x + delta[i][0]y2 = y + delta[i][1]if x2 >= 0 and x2 < len(grid) and y2 >= 0 and y2 < len(grid[0]):  # 判断可否通过那个点if close_matrix[x2][y2] == 0 and grid[x2][y2] == 0:g2 = g + costf2 = g2 + heuristic[x2][y2]cell.append([f2, g2, x2, y2])close_matrix[x2][y2] = 1action_matrix[x2][y2] = iinvpath = []x = target_point[0]y = target_point[1]invpath.append([x, y])  # we get the reverse path from herewhile x != begin_point[0] or y != begin_point[1]:x2 = x - delta[action_matrix[x][y]][0]y2 = y - delta[action_matrix[x][y]][1]x = x2y = y2invpath.append([x, y])path = []for i in range(len(invpath)):path.append(invpath[len(invpath) - 1 - i])return path, action_matrixif __name__ == "__main__":# 0为自由通行节点,1为障碍grid = [[0, 1, 0, 0, 0, 0],[0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0],[0, 1, 0, 0, 1, 0],[0, 0, 0, 0, 1, 0]]begin = [3, 0]  # 3行0列为起始点target = [1, 4]  # 1行4列为终点a_star_path, action_matrix = a_star_search(grid, begin, target)for path in a_star_path:print(path)

A star 算法 (Python)相关推荐

  1. 匈牙利算法python代码实现以及原理图解

    匈牙利算法python代码实现以及原理图解 1.匈牙利算法python代码实现: 2.原理图解: 1.匈牙利算法python代码实现: scipy中有对应的接口scipy.optimize.linea ...

  2. 算法(Python版)|156Kstars|神级项目-(1)The Algorithms - Python简介

    文章目录 算法(Python版) 项目地址 项目概况 说明 参与入门 社区频道 算法列表 Arithmetic Analysis 算术分析 Audio Filters 音频过滤器 Backtracki ...

  3. 棋盘最短路径 python_Dijkstra 最短路径算法 Python 实现

    Dijkstra 最短路径算法 Python 实现 问题描述 使用 Dijkstra 算法求图中的任意顶点到其它顶点的最短路径(求出需要经过那些点以及最短距离). 以下图为例: 算法思想 可以使用二维 ...

  4. 2021-03-15 数据挖掘算法—K-Means算法 Python版本

    数据挖掘算法-K-Means算法 Python版本 简介 又叫K-均值算法,是非监督学习中的聚类算法. 基本思想 k-means算法比较简单.在k-means算法中,用cluster来表示簇:容易证明 ...

  5. 2021-01-28 粒子群优化算法-Python版本和Matlab函数 particleswarm 调用

    粒子群优化算法-Python版本和Matlab函数 particleswarm 调用 前两天分享了粒子群优化算法的原理和Matlab原理实现,本文分享一下Python代码下的PSO实现以及Matlab ...

  6. 最优化算法python实现篇(4)——无约束多维极值(梯度下降法)

    最优化算法python实现篇(4)--无约束多维极值(梯度下降法) 摘要 算法简介 注意事项 算法适用性 python实现 实例运行结果 算法过程可视化 摘要 本文介绍了多维无约束极值优化算法中的梯度 ...

  7. 最优化算法python实现篇(3)——无约束一维极值(黄金分割法)

    最优化算法python实现篇(3)--无约束一维极值(黄金分割法) 算法适用问题 python实现 示例运行结果 算法适用问题 搜索给定单峰区间的极值问题,一般对凸优化问题比较适用. python实现 ...

  8. 最优化算法python实现篇(2)—无约束一维极值(二分法)

    最优化算法python实现篇(2)--无约束一维极值(二分法) 算法适用问题 python实现 示例运行结果 算法适用问题 搜索给定单峰区间的极值问题,一般对凸优化问题比较适用. python实现 # ...

  9. 多元线性回归算法python实现_手写算法-Python代码推广多元线性回归

    1.梯度下降-矩阵形式 上篇文章介绍了一元线性回归,包括Python实现和sklearn实现的实例.对比,以及一些问题点,详情可以看这里: 链接: 手写算法-Python代码实现一元线性回归 里面封装 ...

最新文章

  1. hashmap value可以为空吗_美团面试题:Hashmap结构,1.7和1.8有哪些区别(最详细解析)...
  2. python实现词语填空_python简单实现新词发现
  3. python怎么从键盘输入两个数然后求和并输出_C语音的题:从键盘输入两个整数,要求求和然后输出和。应该怎么做?...
  4. Android 数据加密算法 Des,Base64详解
  5. 2013年全球ERP市场格局(Gartner)
  6. 写在《ASP.NET MVC 4 Web 编程》即将出版之际!献给有节操的程序员!
  7. 10款精选的用于构建良好易用性网站的jQuery插件
  8. 利用深度学习从大脑活动合成语言,应对人类语音挑战
  9. 目标反射回波检测算法及其FPGA实现 之二:互相关/卷积/FIR电路的实现
  10. 消息中间件activemq-5.13.0安全验证配置
  11. Java Set集合及Map集合详解
  12. Python 的切片语法为什么不会出现索引越界呢?
  13. 从入门到放弃C语言-入门篇(1)
  14. Jmeter使用函数助手生成随机数,处理同一个随机数多处使用
  15. mongodb 数据库迁移
  16. java学习——java面试【事务、锁、多线程】资料整理
  17. 爬取《西游记》小说所有章节内容
  18. Unity 自带函数 Reset() 的使用
  19. 2022-2028全球与中国商用车辆HMI解决方案市场现状及未来发展趋势
  20. 盯上年轻人的今日头条,重新以内容出发还有多少可能?

热门文章

  1. IDC:2017年中国网络安全市场分析与2018年预测
  2. H5 js 处理localstorage方法封装
  3. UVA 10886 Standard Deviation
  4. QA专题阅读小组 | 每周一起读 #09
  5. 网页表单form中提交的两种方式
  6. keystore是个嘛东西
  7. spring-注解实现入门
  8. MATLAB符号运算(3)
  9. Mike Krueger 加入Mono团队
  10. 邮箱回执怎么看_考研复试联系导师邮件怎么写?