P1699 Bucket Brigade B 题解
MilkTea_NY · · 题解
P1699 Bucket Brigade B 题解
部分引自魏梦舒的《漫画算法 Python 版》(实际上两个讲的是一样的,不过书本更详细)。
题意
给定一个迷宫,包含起点 B、障碍物 R、终点 L,求出从起点到达终点所需的最小步数(最短路径)。
思路
这里运用到了一种启发式搜索算法:A* 寻路算法。
A* 搜寻算法俗称 A 星算法。A* 算法是比较流行的启发式搜索算法之一,被广泛应用于路径优化领域。它的独特之处是检查最短路径中每个可能的节点时引入了全局信息,对当前节点距终点的距离做出估计,并作为评价该节点处于最短路线上的可能性的量度。
首先要引入两个集合和一个公式:
一个公式如下:
每一个格子都具有
现在有下面三个步骤:
-
第一步:把起点放入
openList ,也就是可到达格子的集合。 -
第二步:找出
openList 中F 值最小的方格作为当前方格。 -
第三步:找出当前方格上下左右所有可到达的格子,看它们是否在
openList 或者closeList 中,如果不在,则将它们加入openList 中。计算出相应G 、H 、F 值,并把当前格子作为它们的父节点。
之后进行第二轮,我们需要一次又一次重复刚刚的第二不和第三步,直到直到终点为止。
代码
这里没有递归的函数,回溯在循环中进行。
def search(start, end):
# 待访问的格子
open_lt = []
# 已访问的格子
close_lt = []
# 把起点加入 open_lt
open_lt.append(start)
# 主循环,每一轮检查一个当前方格节点
while len(open_lt) > 0:
# 在 open_lt 中查找 F 值最小的节点作为当前方格节点
current_grid = find_min_gird(open_lt)
# 当前方格节点从 openList 中移除
open_lt.remove(current_grid)
# 当前方格节点进入 closeList
close_lt.append(current_grid)
# 找到所有邻近节点
neighbors = find_neighbors(current_grid, open_lt, close_lt)
for grid in neighbors:
if grid not in open_lt:
# 邻近节点不在 openList 中,标记父亲、G、H、F,并放入 openList
grid.init_grid(current_grid, end)
open_lt.append(grid)
# 如果终点在 openList 中,直接返回终点格子
for grid in open_lt:
if (grid.x == end.x) and (grid.y == end.y):
return grid
# openList 用尽,仍然找不到终点,说明终点不可到达,返回空
return None
def find_min_gird(open_lt=[]):
temp_grid = open_lt[0]
for grid in open_lt:
if grid.f < temp_grid.f:
temp_grid = grid
return temp_grid
def find_neighbors(grid, open_lt=[], close_lt=[]):
grid_list = []
if is_valid_grid(grid.x, grid.y-1, open_lt, close_lt):
grid_list.append(Grid(grid.x, grid.y-1))
if is_valid_grid(grid.x, grid.y+1, open_lt, close_lt):
grid_list.append(Grid(grid.x, grid.y+1))
if is_valid_grid(grid.x-1, grid.y, open_lt, close_lt):
grid_list.append(Grid(grid.x-1, grid.y))
if is_valid_grid(grid.x+1, grid.y, open_lt, close_lt):
grid_list.append(Grid(grid.x+1, grid.y))
return grid_list
def is_valid_grid(x, y, open_lt=[], close_lt=[]):
# 是否超过边界
if x < 0 or x >= len(MAZE) or y < 0 or y >= len(MAZE[0]):
return False
# 是否有障碍物
if MAZE[x][y]=='R':
return False
# 是否已经在open_lt中
if contain_grid(open_lt, x, y):
return False
# 是否已经在closeList中
if contain_grid(close_lt, x, y):
return False
return True
def contain_grid(grids, x, y):
for grid in grids:
if (grid.x == x) and (grid.y == y):
return True
return False
class Grid:
# 初始化
def __init__(self, x, y):
self.x = x
self.y = y
self.f = 0
self.g = 0
self.h = 0
self.parent = None
def init_grid(self, parent, end):
self.parent = parent
if parent is not None:
self.g = parent.g + 1
else:
self.g=1
self.h = abs(self.x - end.x) + abs(self.y - end.y)
self.f = self.g + self.h
# 迷宫地图
MAZE = []
# 输入迷宫地图
for i in range(10):
MAZE.append(list(input()))
def find(l):
# 在迷宫中寻找起点和终点下标的函数
for i in range(10):
# 如果要寻找的函数在 MAZE[i] 里面
if l in MAZE[i]:
x = i # 返回起点/终点所在的列
for j in range(10):
if MAZE[i][j]==l: # 返回终点所在的行数下标
return (x,j)
# 寻找起点和终点所在的下标
B = find('B')
L = find('L')
# 设置起点和终点
start_grid = Grid(B[0],B[1]) # 起点的行与列
end_grid = Grid(L[0],L[1]) # 终点的行与列
# 搜索迷宫终点
result_grid = search(start_grid, end_grid)
# 回溯迷宫路径
path = []
while result_grid is not None:
path.append(Grid(result_grid.x, result_grid.y))
result_grid = result_grid.parent
ans = 0
# 输出迷宫和路径,路径用 @ 号表示
for i in range(0, len(MAZE)):
for j in range(0, len(MAZE[0])):
if contain_grid(path, i, j): #可行的路径
# print("@", end=' ')
ans+=1
# else:
# print(str(MAZE[i][j]), end=' ')
# print()
print(ans-2) # 由于包括起点 B 和终点 L,所以减去 2
结尾
将代码中最后几行代码代码去掉注释就可以输出最短路径了。
好了,感谢您观看到这,谢谢观看!
本文皆为 Python 代码,如果有兴趣的可以了解更多!
管理员、志愿者们辛苦了!
(本文过审当天经过修改)。