这是一个使用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. jmter提交图片

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

  2. Introduction to MWB Minor Mode

    Introduction to MWB Minor Mode */--> Table of Contents 1. Introduction 2. Usage 1 Introduction MW ...

  3. windows 依赖查看

    使用工具Download Process Explorer查看运行程序所依赖的动态库. 中文说明:适用于 Windows 的 Process Explorer 10.21 版

  4. CentOS下编译安装python包管理安装工具pip教程

    ubuntu 安装pip 代码如下: apt-get install python-pip 安装requests, pip install requests 对于centos的,直接 yum inst ...

  5. html-图片热点和网页划区

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

  6. Python3语法详解

    一.下载安装 1.1Python下载 Python官网:https://www.python.org/ 1.2Python安装 1.2.1 Linux 平台安装 以下为在Unix & Linu ...

  7. 【BZOJ】1566: [NOI2009]管道取珠

    题解 假如我们非常熟练的看出来,平方和转有序对统计的套路的话,应该就不难了 我们只需要统计(wayA,wayB)生成的序列一样的有序对个数就行 可以用一个\(n^3\)的dp解决 \(dp[i][j] ...

  8. jQuery EasyUI-DataGrid动态加载表头

    项目总结—jQuery EasyUI-DataGrid动态加载表头     目录(?)[-] 概要 实现 总结   概要 在前面两篇文章中,我们已经介绍了在jQuery EasyUI-DataGrid ...

  9. Loadrunner乱码问题

    在LoadRunner中录制脚本时,出现乱码的问题解决 我在录制一个Web的脚本时,出现中文乱码. 原因为Web中采用的是UTF-8编码,而录制脚本的选项默认没有把支持UTF8选中. 方法:1. To ...

  10. ECshop语言包lang的加载原理

    当前使用的ecshop的版本:2.7.3,ecshop 2.7.3版本的网店系统的语言包的位置是ecshop文件下 languages/xxx/   其中的xxx表示各种语言的文件夹,里面存放指定语言 ...