이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
# https://oj.uz/problem/view/BOI13_tracks
from collections import deque
h, w = map(int, input().split())
grid = []
for _ in range(h):
grid.append(input())
def invert(c):
return 'R' if c == 'F' else 'F'
tracks = sum(sum(col != '.' for col in row) for row in grid)
ans = 0
seen = set()
waiting = set([(0, 0)])
waiting_color = grid[0][0]
while len(seen) < tracks:
ans += 1
Q = deque(waiting)
waiting.clear()
while Q:
r, c = Q.popleft()
seen.add((r, c))
for nr, nc in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]:
if 0 <= nr < h and 0 <= nc < w and (nr, nc) not in seen:
if grid[nr][nc] == waiting_color:
Q.append((nr, nc))
elif grid[nr][nc] == invert(waiting_color):
waiting.add((nr, nc))
waiting_color = invert(waiting_color)
print(ans)
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |