제출 #521117

#제출 시각아이디문제언어결과실행 시간메모리
521117zaystevTracks in the Snow (BOI13_tracks)Cpython 3
49.69 / 100
2101 ms311188 KiB
from collections import deque m, n = map(int, input().strip().split()) grid = [] for _ in range(m): grid.append(list(input().strip())) q = deque([(0, 0)]) visited = set() costs = [[float("inf")] * n for _ in range(m)] costs[0][0] = 1 ans = 1 while q: x, y = q.popleft() ans = max(ans, costs[x][y]) visited.add((x, y)) for (dx, dy) in [(0, 1), (0, -1), (1, 0), (-1, 0)]: n_x, n_y = x + dx, y + dy if 0 <= n_x < m and 0 <= n_y < n and grid[n_x][n_y] != "." and (n_x, n_y) not in visited: loss = 0 if grid[n_x][n_y] == grid[x][y] else 1 if loss + costs[x][y] < costs[n_x][n_y]: costs[n_x][n_y] = loss + costs[x][y] q.append((n_x, n_y)) if loss == 1 else q.appendleft((n_x, n_y)) print(ans)
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...