每一次他可以选择花费一个时间单位向上或向下或向左或向右走一格,或是使用自己的对称飞行器花费一个时间单位瞬移到关于当前自己点中心对称的格子,且每一次移动的目的地不能存在障碍物。
具体来说,设当前迷宫有
需要注意的是,对称飞行器最多使用
第一行两个空格分隔的正整数,分别代表迷宫的行数和列数。
接下来行 每行一个长度为
的字符串来描述这个迷宫。
其中代表通路。
代表障碍。
代表起点。
代表终点。
保证只有一个和 一个
。
仅一行一个整数表示从起点最小花费多少时间单位到达终点。
如果无法到达终点,输出。
4 4 #S.. E#.. #... ....
4
一种可行的路径是用对称飞行器到达再向上走一步,再向右走一步,然后使用一次对称飞行器到达终点。
BFS
from collections import deque n, m = map(int, input().split()) migong = [input() for _ in range(n)] S = (0, 0), 0 E = (0, 0) for i, row in enumerate(migong, 1): for j, col in enumerate(row, 1): if col == "S": S = (i, j), 0 if col == "E": E = (i, j) q = deque([S]) direction = [(-1, 0), (0, 1), (1, 0), (0, -1)] isvisited = set([S[0]]) def move(pos: tuple[tuple[int, int], int], dir: tuple[int, int]): (x, y), _ = pos dx, dy = dir return (x + dx, y + dy), _ def jmp(pos: tuple[tuple[int, int], int]): (x, y), _ = pos return (n + 1 - x, m + 1 - y), _ + 1 def check(pos: tuple[tuple[int, int], int]): (x, y), times = pos return ( 1 <= x <= m and 1 <= y <= n and migong[x - 1][y - 1] != "#" and times <= 5 and (x, y) not in isvisited ) # print(migong, E, S) cnt = 0 success = False while q: l = len(q) for _ in range(l): t = q.pop() # print(t) if t[0] == E: print(cnt) success = True break for j in direction: tt = move(t, j) # print(tt) if check(tt): # print(tt) isvisited.add(tt[0]) q.appendleft(tt) jmpt = jmp(t) # print(jmpt) if check(jmpt): q.appendleft(jmpt) isvisited.add(jmpt[0]) cnt += 1 if not success: print(-1) """ 4 4 #S.. E#.. #... .... """