这是一个使用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算法解迷宫的更多相关文章

  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. KVM和远程管理工具virt-manager

    kvm在server端的部署(针对rhel6系统,可以构建本地更新源) 注意:如果只是安装管理工具,可以试试直接执行8步骤 1.对服务器实行kvm虚拟化首先需要确认服务器的物理硬件是否支持 cat / ...

  2. jmter提交图片

    jmter提交图片 https://www.cnblogs.com/linglingyuese/p/4514808.html

  3. No.4 selenium学习之路之iframe

    查看iframe: 1.top window ——可以直接进行定位

  4. js字符串基本操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtm ...

  5. UVA10212 【The Last Non-zero Digit.】

    暴力可做!!!(十秒还不打暴力!!!)暴力算阶乘边算边取余上代码 #include<iostream> #define int long long //开long long using n ...

  6. java根据日期获取周几和获取某段时间内周几的日期

    整理两个日期的方法. 根据日期获取当天是周几 /** * 根据日期获取当天是周几 * @param datetime 日期 * @return 周几 */ public static String d ...

  7. sicily 1046. Plane Spotting(排序求topN)

    DescriptionCraig is fond of planes. Making photographs of planes forms a major part of his daily lif ...

  8. jquery选择里存在特殊字符,需要加双转义字符

    //元素为:<input type="checkbox" value="abc/index" /> //处理选择器转义问题 //去除值 $val = ...

  9. Adobe Photoshop CC 2017-18.0安装教程

    Adobe Photoshop CC 2017-18.0安装教程 注:下载链接在文章后面 第一步:首先请将电脑的网络断开,很简单:禁用本地连接或者拔掉网线,这样就可以免除登录Creative Clou ...

  10. Python2字符编码问题汇总

    目录 从字符编码说起 unicode与utf-8 当编解码遇上Python2.x unicode 与 str 区别 __str__ __repr__的区别 unicode str utf-8关系 un ...