This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
# 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())
ans = 0
Q = deque([(0, 0, 1)])
seen = set()
while Q:
r, c, depth = Q.popleft()
seen.add((r, c))
ans = max(ans, depth)
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 and grid[nr][nc] != '.':
if grid[nr][nc] == grid[r][c]:
Q.appendleft((nr, nc, depth))
else:
Q.append((nr, nc, depth + 1))
print(ans)
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |