from collections import deque h, w = map(int, input().split()) S = [[[c, 10**9] for c in…
- User
from collections import deque
h, w = map(int, input().split()) S = [[[c, 10**9] for c in input()] for _ in range(h)]
E = [] for i in range(h): for j in range(w): if S[i][j][0] == "E": E.append((i, j)) S[i][j] = ["E", 0]
def bfs(start): queue = deque([start]) directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] symbols = ['v', '^', '>', '<'] while queue: x, y = queue.popleft() for (dx, dy), dir in zip(directions, symbols): for dx, dy, dir in ((-1, 0, 'v'), (1, 0, '^'), (0, -1, '>'), (0, 1, '<')): nx, ny = x + dx, y + dy if 0 <= nx < h and 0 <= ny < w and S[nx][ny][1] > S[x][y][1] + 1 and S[nx][ny][0] != "#" and S[nx][ny][0] != "E": queue.append((nx, ny)) S[nx][ny][1] = S[x][y][1] + 1 S[nx][ny][0] = dir
for start in E: bfs(start)
for row in S: for cell in row: if cell[0] == "E": print("E", end="") elif cell[0] == "#": print("#", end="") else: print(cell[0], end="") print()
計算量の改善はできますか?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.