Created
December 21, 2025 01:09
-
-
Save maehrm/7d30f0d7a37ee20919ca85fc535bd7bf to your computer and use it in GitHub Desktop.
D - Escape Route https://atcoder.jp/contests/abc405/tasks/abc405_d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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