zoukankan      html  css  js  c++  java
  • 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 = 0
            self.h = 0
            self.f = 0
            
    class AStar(object):
        def __init__(self):
            self.op = []
            heapq.heapify(self.op)
            self.cl = set()
            self.cells = []
            self.gridHeight = 6
            self.gridWidth = 6
            
        def init_grid(self):
            walls = ((0, 5), (1, 0), (1, 1), (1, 5), (2, 3),
                     (3, 1), (3, 2), (3, 5), (4, 1), (4, 4), (5, 1))
            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(0, 0)
            self.end = self.get_cell(5, 5)
            
        def get_heuristic(self, cell):
            return 10 * (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-1:
                cells.append(self.get_cell(cell.x+1, cell.y))
            if cell.y > 0:
                cells.append(self.get_cell(cell.x, cell.y-1))
            if cell.x > 0:
                cells.append(self.get_cell(cell.x-1, cell.y))
            if cell.y < self.gridHeight-1:
                cells.append(self.get_cell(cell.x, cell.y+1))
            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 + 10
            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 + 10:
                                    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()
  • 相关阅读:
    1.xposed框架简介
    Spinner android:entries属性
    nginx 负载均衡时,一台tomcat宕机时的问题 自动切换(转自java版web项目-微信公众号)
    java内存模型(JMM)之happens-before
    java中Infinity(无限)和NaN
    mysql数据库相关操作
    提高mysql数据库查询效率
    转载-----通过xml处理sql语句时对小于号与大于号的处理转换
    JAVA过滤器和springMVC拦截器的区别
    Java中的Filter过滤器
  • 原文地址:https://www.cnblogs.com/qq78292959/p/3490115.html
Copyright © 2011-2022 走看看