import heapqdef min_steps_to_destination(m, n, T, grid):# 定义四个方向移动directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]# 优先队列,格式:(步数, x, y, 剩余爆发次数)pq = [(0, 0, 0, 3)] # 初始位置 (0,0),步数为 0,爆发次数 3visited = set # 记录访问过的状态while pq:steps, x, y, burst = heapq.heappop(pq)# 如果到达终点,返回步数if x == m - 1 and y == n - 1:return steps# 记录当前状态if (x, y, burst) in visited:continuevisited.add((x, y, burst))# 遍历四个方向for dx, dy in directions:nx, ny = x + dx, y + dyif 0 0:heapq.heappush(pq, (steps + 1, nx, ny, burst - 1))return -1 # 无法到达终点# 读取输入m, n, T = map(int, input.split)grid = [list(map(int, input.split)) for _ in range(m)]# 计算最短步数print(min_steps_to_destination(m, n, T, grid))建图:摘要:import heapqdef min_steps_to_destination(m, n, T, grid):# 定义四个方向移动directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]# 优先队列,格式:(步数, x,
读取 m × n 的矩阵。
每个格子的数值表示 高度。
只能上下左右移动。
BFS + 优先队列普通移动时,高度差不能超过1。
如果高度差 超过1但小于等于T,可使用 爆发技能(最多3次)。
使用 队列(heapq) 进行搜索,记录 (当前步数, x, y, 剩余爆发次数)。
终点条件到达右下角 (m-1, n-1) 结束。
来源:建琛教育
免责声明:本站系转载,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本站联系,我们将在第一时间删除内容!