这是一个使用A* search算法解迷宫的问题,细节请看:http://www.laurentluce.com/posts/solving-mazes-using-python-simple-recursivity-and-a-search/

Laurent Luce的A* search算法有点问题,我这边运行是死循环,稍微修改了一下。

  1. import heapq
  2.  
  3. class Cell(object):
  4. def __init__(self, x, y, reachable):
  5. self.reachable = reachable
  6. self.x = x
  7. self.y = y
  8. self.parent = None
  9. self.g =
  10. self.h =
  11. self.f =
  12.  
  13. class AStar(object):
  14. def __init__(self):
  15. self.op = []
  16. heapq.heapify(self.op)
  17. self.cl = set()
  18. self.cells = []
  19. self.gridHeight =
  20. self.gridWidth =
  21.  
  22. def init_grid(self):
  23. walls = ((, ), (, ), (, ), (, ), (, ),
  24. (, ), (, ), (, ), (, ), (, ), (, ))
  25. for x in range(self.gridWidth):
  26. for y in range(self.gridHeight):
  27. if (x, y) in walls:
  28. reachable = False
  29. else:
  30. reachable = True
  31. self.cells.append(Cell(x, y, reachable))
  32. self.start = self.get_cell(, )
  33. self.end = self.get_cell(, )
  34.  
  35. def get_heuristic(self, cell):
  36. return * (abs(cell.x - self.end.x) + abs(cell.y - self.end.y))
  37.  
  38. def get_cell(self, x, y):
  39. return self.cells[x * self.gridHeight + y]
  40.  
  41. def get_adjacent_cells(self, cell):
  42. cells = []
  43. if cell.x < self.gridWidth-:
  44. cells.append(self.get_cell(cell.x+, cell.y))
  45. if cell.y > :
  46. cells.append(self.get_cell(cell.x, cell.y-))
  47. if cell.x > :
  48. cells.append(self.get_cell(cell.x-, cell.y))
  49. if cell.y < self.gridHeight-:
  50. cells.append(self.get_cell(cell.x, cell.y+))
  51. return cells
  52.  
  53. def display_path(self):
  54. cell = self.end
  55. while cell.parent is not self.start:
  56. cell = cell.parent
  57. print 'path: cell: %d,%d' % (cell.x, cell.y)
  58.  
  59. def update_cell(self, adj, cell):
  60. adj.g = cell.g +
  61. adj.h = self.get_heuristic(adj)
  62. adj.parent = cell
  63. adj.f = adj.h + adj.g
  64.  
  65. def process(self):
  66. heapq.heappush(self.op, (self.start.f, self.start))
  67. while len(self.op):
  68. f, cell = heapq.heappop(self.op)
  69. self.cl.add(cell)
  70. if cell is self.end:
  71. self.display_path()
  72. break
  73. adj_cells = self.get_adjacent_cells(cell)
  74. for c in adj_cells:
  75. if c.reachable:
  76. if c in self.cl:
  77. if (c.f, c) in self.op:
  78. if c.g > cell.g + :
  79. self.update_cell(c, cell)
  80. else:
  81. self.update_cell(c, cell)
  82. heapq.heappush(self.op, (c.f, c))
  83.  
  84. if __name__ == "__main__":
  85. a = AStar()
  86. a.init_grid()
  87. a.process()

A* search算法解迷宫的更多相关文章

  1. 剑指Offer——回溯算法解迷宫问题(java版)

    剑指Offer--回溯算法解迷宫问题(java版)   以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍.设计程序,对任意设定的迷宫,求出从入口到出口的所有通路.   下面我们来详细讲一 ...

  2. 从vector容器中查找一个子串:search()算法

    如果要从vector容器中查找是否存在一个子串序列,就像从一个字符串中查找子串那样,次数find()与find_if()算法就不起作用了,需要采用search()算法:例子: #include &qu ...

  3. Breadth-first search 算法(Swift版)

    在讲解Breadth-first search 算法之前,我们先简单介绍两种数据类型Graph和Queue. Graph 这就是一个图,它由两部分组成: 节点, 使用圆圈表示的部分 边, 使用线表示的 ...

  4. 对《禁忌搜索(Tabu Search)算法及python实现》的修改

    这个算法是在听北大人工智能mooc的时候,老师讲的一种局部搜索算法,可是举得例子不太明白.搜索网页后,发现<禁忌搜索(Tabu Search)算法及python实现>(https://bl ...

  5. Prim算法生成迷宫

    初始化地图 function initMaze(r,c){ let row = new Array(2 * r + 1) for(let i = 0; i < row.length; i++){ ...

  6. A* search算法

    今天,还是国庆和中秋双节的时间节点,一个天气不错的日子,孩子已经早早的睡觉了,玩了一整天,也不睡觉,累的实在扛不住了,勉强洗澡结束,倒床即睡着的节奏... 不多说题外话,进入正题. 什么是A*搜索算法 ...

  7. 【优化算法】Greedy Randomized Adaptive Search算法 超详细解析,附代码实现TSP问题求解

    01 概述 Greedy Randomized Adaptive Search,贪婪随机自适应搜索(GRAS),是组合优化问题中的多起点元启发式算法,在算法的每次迭代中,主要由两个阶段组成:构造(co ...

  8. Dijkstra算法初步 - 迷宫问题

    你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数.还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间.游戏规定了 ...

  9. [迷宫中的算法实践]迷宫生成算法——递归分割算法

    Recursive division method        Mazes can be created with recursive division, an algorithm which wo ...

随机推荐

  1. 07 go语言

    Home   Alexey Palazhchenko edited this page on 9 Jul · 89 revisions Welcome to the Go wiki, a collec ...

  2. 移动端默认meta标签

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><met ...

  3. C#中ASCII码与字符串的互换

    主要代码: int a = (int)'a';// 把字符直接转换为int类型,即可得到ASCII码值 ).ToString();// 将数字直接转换为char类型,即可得到ASCII码对应的字符 C ...

  4. Asp.net Vnext 实现IView

    概述 Iview定义很简单,就是根据View上下文和TextWriter对象实现对View的呈现. 实现 实现IViewEngine public class TestViewEngine : IVi ...

  5. centos7 配置ftp访问

    vsftp安装 创建用户 并限定用户在自己的目录 1.查看是否已经安装了vsftpd vsftpd -version 2.安装vsftpd(CentOS7) yum install -y vsftpd ...

  6. 关于C语言的几个考试编程题目

    提交要求:1:邮件名称:学号后三位-题目编号-姓名-期中考试.例如:098-1-沈苗-期中考试2:不用附件提交,直接写邮件,内容包括编程思路(写一段自己对题目的认识.思路.技术细节等).源代码.运行结 ...

  7. 【LOJ】#2039. 「SHOI2015」激光发生器

    题解 我永远都写不对计算几何-- 首先找到反射的线段比较好找,扫一遍所有线段然后找交点在镜子上并且交点离起点最近的那条线段 然后旋转的时候,有可能是顺时针,也有可能是逆时针,要找出法线,然后判断法线和 ...

  8. 纯CSS实现3D图像轮转

    CSS演武场今天继续,今天看一个纯css实现的3D图像轮转效果,请大家猛戳研究效果先,也可下载收藏先. 首先看html文件,div.billboard为效果的容器,利用10个div.poster分割图 ...

  9. 装饰 Markdown

    利用 Font Awesome 提升 Markdown 的表现能力 Font Awesome 是一个字体和图标工具包,包含人物.动物.建筑.商业.品牌等等各种主题丰富的图标符号,可以通过相应的语法添加 ...

  10. java main class not found

    1.确保 所有jar都存在, 清理所有不存在的jar 2.确保src以外没有java类