A* search算法解迷宫
这是一个使用A* search算法解迷宫的问题,细节请看:http://www.laurentluce.com/posts/solving-mazes-using-python-simple-recursivity-and-a-search/
Laurent Luce的A* search算法有点问题,我这边运行是死循环,稍微修改了一下。
- import heapq
- class Cell(object):
- def __init__(self, x, y, reachable):
- self.reachable = reachable
- self.x = x
- self.y = y
- self.parent = None
- self.g =
- self.h =
- self.f =
- class AStar(object):
- def __init__(self):
- self.op = []
- heapq.heapify(self.op)
- self.cl = set()
- self.cells = []
- self.gridHeight =
- self.gridWidth =
- def init_grid(self):
- walls = ((, ), (, ), (, ), (, ), (, ),
- (, ), (, ), (, ), (, ), (, ), (, ))
- for x in range(self.gridWidth):
- for y in range(self.gridHeight):
- if (x, y) in walls:
- reachable = False
- else:
- reachable = True
- self.cells.append(Cell(x, y, reachable))
- self.start = self.get_cell(, )
- self.end = self.get_cell(, )
- def get_heuristic(self, cell):
- return * (abs(cell.x - self.end.x) + abs(cell.y - self.end.y))
- def get_cell(self, x, y):
- return self.cells[x * self.gridHeight + y]
- def get_adjacent_cells(self, cell):
- cells = []
- if cell.x < self.gridWidth-:
- cells.append(self.get_cell(cell.x+, cell.y))
- if cell.y > :
- cells.append(self.get_cell(cell.x, cell.y-))
- if cell.x > :
- cells.append(self.get_cell(cell.x-, cell.y))
- if cell.y < self.gridHeight-:
- cells.append(self.get_cell(cell.x, cell.y+))
- return cells
- def display_path(self):
- cell = self.end
- while cell.parent is not self.start:
- cell = cell.parent
- print 'path: cell: %d,%d' % (cell.x, cell.y)
- def update_cell(self, adj, cell):
- adj.g = cell.g +
- adj.h = self.get_heuristic(adj)
- adj.parent = cell
- adj.f = adj.h + adj.g
- def process(self):
- heapq.heappush(self.op, (self.start.f, self.start))
- while len(self.op):
- f, cell = heapq.heappop(self.op)
- self.cl.add(cell)
- if cell is self.end:
- self.display_path()
- break
- adj_cells = self.get_adjacent_cells(cell)
- for c in adj_cells:
- if c.reachable:
- if c in self.cl:
- if (c.f, c) in self.op:
- if c.g > cell.g + :
- self.update_cell(c, cell)
- else:
- self.update_cell(c, cell)
- heapq.heappush(self.op, (c.f, c))
- if __name__ == "__main__":
- a = AStar()
- a.init_grid()
- a.process()
A* search算法解迷宫的更多相关文章
- 剑指Offer——回溯算法解迷宫问题(java版)
剑指Offer--回溯算法解迷宫问题(java版) 以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍.设计程序,对任意设定的迷宫,求出从入口到出口的所有通路. 下面我们来详细讲一 ...
- 从vector容器中查找一个子串:search()算法
如果要从vector容器中查找是否存在一个子串序列,就像从一个字符串中查找子串那样,次数find()与find_if()算法就不起作用了,需要采用search()算法:例子: #include &qu ...
- Breadth-first search 算法(Swift版)
在讲解Breadth-first search 算法之前,我们先简单介绍两种数据类型Graph和Queue. Graph 这就是一个图,它由两部分组成: 节点, 使用圆圈表示的部分 边, 使用线表示的 ...
- 对《禁忌搜索(Tabu Search)算法及python实现》的修改
这个算法是在听北大人工智能mooc的时候,老师讲的一种局部搜索算法,可是举得例子不太明白.搜索网页后,发现<禁忌搜索(Tabu Search)算法及python实现>(https://bl ...
- Prim算法生成迷宫
初始化地图 function initMaze(r,c){ let row = new Array(2 * r + 1) for(let i = 0; i < row.length; i++){ ...
- A* search算法
今天,还是国庆和中秋双节的时间节点,一个天气不错的日子,孩子已经早早的睡觉了,玩了一整天,也不睡觉,累的实在扛不住了,勉强洗澡结束,倒床即睡着的节奏... 不多说题外话,进入正题. 什么是A*搜索算法 ...
- 【优化算法】Greedy Randomized Adaptive Search算法 超详细解析,附代码实现TSP问题求解
01 概述 Greedy Randomized Adaptive Search,贪婪随机自适应搜索(GRAS),是组合优化问题中的多起点元启发式算法,在算法的每次迭代中,主要由两个阶段组成:构造(co ...
- Dijkstra算法初步 - 迷宫问题
你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数.还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间.游戏规定了 ...
- [迷宫中的算法实践]迷宫生成算法——递归分割算法
Recursive division method Mazes can be created with recursive division, an algorithm which wo ...
随机推荐
- 07 go语言
Home Alexey Palazhchenko edited this page on 9 Jul · 89 revisions Welcome to the Go wiki, a collec ...
- 移动端默认meta标签
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><met ...
- C#中ASCII码与字符串的互换
主要代码: int a = (int)'a';// 把字符直接转换为int类型,即可得到ASCII码值 ).ToString();// 将数字直接转换为char类型,即可得到ASCII码对应的字符 C ...
- Asp.net Vnext 实现IView
概述 Iview定义很简单,就是根据View上下文和TextWriter对象实现对View的呈现. 实现 实现IViewEngine public class TestViewEngine : IVi ...
- centos7 配置ftp访问
vsftp安装 创建用户 并限定用户在自己的目录 1.查看是否已经安装了vsftpd vsftpd -version 2.安装vsftpd(CentOS7) yum install -y vsftpd ...
- 关于C语言的几个考试编程题目
提交要求:1:邮件名称:学号后三位-题目编号-姓名-期中考试.例如:098-1-沈苗-期中考试2:不用附件提交,直接写邮件,内容包括编程思路(写一段自己对题目的认识.思路.技术细节等).源代码.运行结 ...
- 【LOJ】#2039. 「SHOI2015」激光发生器
题解 我永远都写不对计算几何-- 首先找到反射的线段比较好找,扫一遍所有线段然后找交点在镜子上并且交点离起点最近的那条线段 然后旋转的时候,有可能是顺时针,也有可能是逆时针,要找出法线,然后判断法线和 ...
- 纯CSS实现3D图像轮转
CSS演武场今天继续,今天看一个纯css实现的3D图像轮转效果,请大家猛戳研究效果先,也可下载收藏先. 首先看html文件,div.billboard为效果的容器,利用10个div.poster分割图 ...
- 装饰 Markdown
利用 Font Awesome 提升 Markdown 的表现能力 Font Awesome 是一个字体和图标工具包,包含人物.动物.建筑.商业.品牌等等各种主题丰富的图标符号,可以通过相应的语法添加 ...
- java main class not found
1.确保 所有jar都存在, 清理所有不存在的jar 2.确保src以外没有java类