Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created December 21, 2025 01:09
Show Gist options
  • Select an option

  • Save maehrm/7d30f0d7a37ee20919ca85fc535bd7bf to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/7d30f0d7a37ee20919ca85fc535bd7bf to your computer and use it in GitHub Desktop.
from collections import deque
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
ans = [row[:] for row in S]
dist = [[float("inf")] * W for _ in range(H)]
dq = deque()
for y in range(H):
for x in range(W):
if S[y][x] == "E":
dq.append((x, y))
dist[y][x] = 0
dirs = [(0, 1, "^"), (1, 0, "<"), (0, -1, "v"), (-1, 0, ">")]
while dq:
x, y = dq.popleft()
for dx, dy, arrow in dirs:
nx, ny = x + dx, y + dy
if nx < 0 or nx >= W or ny < 0 or ny >= H:
continue
if S[ny][nx] != ".":
continue
if dist[ny][nx] != float('inf'):
continue
dist[ny][nx] = dist[y][x] + 1
ans[ny][nx] = arrow
dq.append((nx, ny))
for row in ans:
print("".join(row))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment