A*(A星)路径搜索算法属于什么样的算法范式/算法设计范式?
创始人
2024-05-07 22:22:47
0

A*(A星)路径搜索算法属于启发式搜索算法范式。

下面是一个简单的Python代码示例,展示如何使用A*算法搜索最短路径:

class Node:
    def __init__(self, x, y, cost=0, heuristic=0, parent=None):
        self.x = x
        self.y = y
        self.cost = cost
        self.heuristic = heuristic
        self.parent = parent

    def total_cost(self):
        return self.cost + self.heuristic


def heuristic(node, goal):
    return abs(node.x - goal.x) + abs(node.y - goal.y)


def astar_search(start, goal, grid):
    open_list = []
    closed_list = []

    open_list.append(start)

    while open_list:
        current_node = min(open_list, key=lambda node: node.total_cost())

        if current_node == goal:
            path = []
            while current_node:
                path.append((current_node.x, current_node.y))
                current_node = current_node.parent
            return path[::-1]

        open_list.remove(current_node)
        closed_list.append(current_node)

        neighbors = [(current_node.x - 1, current_node.y),
                     (current_node.x + 1, current_node.y),
                     (current_node.x, current_node.y - 1),
                     (current_node.x, current_node.y + 1)]

        for neighbor_x, neighbor_y in neighbors:
            if (neighbor_x < 0 or neighbor_x >= len(grid) or
                    neighbor_y < 0 or neighbor_y >= len(grid[0]) or
                    grid[neighbor_x][neighbor_y] == 1):
                continue

            neighbor = Node(neighbor_x, neighbor_y)
            neighbor.cost = current_node.cost + 1
            neighbor.heuristic = heuristic(neighbor, goal)
            neighbor.parent = current_node

            if neighbor in closed_list:
                continue

            if neighbor in open_list:
                if neighbor.cost < current_node.cost:
                    neighbor.parent = current_node
            else:
                open_list.append(neighbor)

    return None


# 示例使用
grid = [[0, 0, 1, 0, 0],
        [0, 0, 1, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0],
        [0, 0, 0, 0, 0]]

start = Node(0, 0)
goal = Node(4, 4)

path = astar_search(start, goal, grid)
print(path)

该示例中,Node类表示搜索中的节点,heuristic函数计算节点和目标节点之间的启发式估计值,astar_search函数执行A*搜索算法。在示例中,我们使用一个5x5的网格来表示地图,其中0表示可以通过的路径,1表示障碍物。起始节点为(0, 0),目标节点为(4, 4)。最后,通过调用astar_search函数,我们得到了最短路径的坐标序列。

相关内容

热门资讯

玻璃硬盘原理图 玻璃硬盘原理 玻璃硬盘,又称为磁头悬浮硬盘(Magnetic Head Flying Disk,MHFD),是一种...
家里监控最长能保存多少天的记录... 家里监控一般保存多久 随着科技的发展,家庭监控系统已经成为了许多家庭的必备设备,它不仅可以帮助我们...
闲鱼搜索规则与技巧 闲鱼最新特... 在闲鱼这个二手交易平台上,有很多用户都希望能够找到一些特殊的东西,比如一些罕见的收藏品、独特的手工艺...
QQ音乐提示代理模式可能无法正... QQ音乐提示代理模式可能无法正常访问,如上图所示,是怎么回事呢? 这个可能和你的网络设置有关系,首先...
别人打电话听不见我说话怎么回事... 当我们在使用手机时,可能会遇到别人打电话过来听不见声音的情况,这种情况可能是由多种原因导致的,下面我...
华为tag有用吗 华为tag-... 华为Tag是华为手机中的一种功能,它可以帮助用户更好地管理自己的手机数据和应用,通过使用华为Tag,...
ps5手柄可用手机快充充电吗 ... PS5手柄,即PlayStation 5的DualSense手柄,是索尼公司为PlayStation...
frp内网穿透配置 HTTP ... HTTP 类型的代理相比于 TCP 类型,不仅在服务端只需要监听一个额外的端口 vhost_http...
广电4k机顶盒怎么连接 广电网... 四广电网络,即四家主流的广播电视网络运营商,包括中国电信、中国移动、中国联通和中国广电,这些运营商为...
hwid是永久激活吗 hwid... HWID,全称Hardware ID,是硬件识别码的缩写,它是计算机硬件制造商为了区分每一台设备而分...