难得的Python题解(2)
2025-08-12 08:53:19
发布于:上海
import heapq
from typing import List
class State:
def __init__(self, time: int, i: int, j: int):
self.time = time
self.i = i
self.j = j
def __lt__(self, other):
return self.time < other.time
def can_escape(n: int, m: int, A_power: int, H_power: int,
fire: str, p: int, o: int, grid: List[str]) -> str:
dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]
heap = []
dist = [[float('inf')] * m for _ in range(n)]
path_len = [[0] * m for _ in range(n)]
heapq.heappush(heap, State(0, 0, 0))
dist[0][0] = 0
path_len[0][0] = 1
while heap:
curr = heapq.heappop(heap)
if curr.i == n-1 and curr.j == m-1:
hunter_time = curr.time - 1
hunter_steps = hunter_time // o
return "Yes" if path_len[curr.i][curr.j] > hunter_steps else "No"
if curr.time > dist[curr.i][curr.j]:
continue
for di, dj in dirs:
ni, nj = curr.i + di, curr.j + dj
if 0 <= ni < n and 0 <= nj < m:
cell = grid[ni][nj]
added_time = 0
can_pass = True
if cell == '#':
can_pass = False
elif cell == '!':
added_time = 5
elif cell == '\\':
if A_power >= H_power:
added_time = 10
else:
can_pass = False
elif cell == '%':
if fire == '^':
can_pass = False
elif cell == '*':
added_time = 3
elif cell == '$':
added_time = 1
if can_pass:
new_time = curr.time + added_time
if new_time < dist[ni][nj]:
dist[ni][nj] = new_time
path_len[ni][nj] = path_len[curr.i][curr.j] + 1
heapq.heappush(heap, State(new_time, ni, nj))
return "No"
if __name__ == "__main__":
n, m = map(int, input().split())
A_power, H_power = map(int, input().split())
fire = input().strip()
p, o = map(int, input().split())
grid = [input().strip() for _ in range(n)]
print(can_escape(n, m, A_power, H_power, fire, p, o, grid))
全部评论 5
6
3天前 来自 浙江
16
2天前 来自 上海
0
6
13小时前 来自 上海
06
2天前 来自 上海
0为啥我发的题解大家评论都是‘6’!
我要黑化!
2天前 来自 上海
02天前 来自 上海
06
2天前 来自 上海
066666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666
2天前 来自 上海
0
2天前 来自 上海
0
有帮助,赞一个