Submission #626070

#TimeUsernameProblemLanguageResultExecution timeMemory
62607054skyxenonMecho (IOI09_mecho)Cpython 3
19 / 100
1099 ms19616 KiB
# https://oj.uz/problem/view/IOI09_mecho from collections import deque from math import ceil n, s = map(int, input().split()) forest = [] for _ in range(n): forest.append(input()) start = None end = None hives = [] for i in range(n): for j in range(n): if forest[i][j] == 'M': start = (i, j) if forest[i][j] == 'D': end = (i, j) if forest[i][j] == 'H': hives.append((i, j)) dist_hive = [[float('inf')] * n for _ in range(n)] hives_Q = deque() for i, j in hives: dist_hive[i][j] = 0 hives_Q.append((i, j, 0)) while hives_Q: r, c, depth = hives_Q.popleft() for nr, nc in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]: if 0 <= nr < n and 0 <= nc < n and forest[nr][nc] == 'G' and dist_hive[nr][nc] == float('inf'): hives_Q.append((nr, nc, depth + 1)) dist_hive[nr][nc] = depth + 1 def bs(lo, hi): global dist_hive def ok(midd): seen = [[False] * n for _ in range(n)] seen[start[0]][start[1]] = True mecho_Q = deque([(start[0], start[1], 0)]) while mecho_Q: r, c, depth = mecho_Q.popleft() if (r, c) == end: return True for nr, nc in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]: if 0 <= nr < n and 0 <= nc < n and forest[nr][nc] in {'G', 'D'} and (midd + ((depth + 1) // s)) < dist_hive[nr][nc] and not seen[nr][nc]: seen[nr][nc] = True mecho_Q.append((nr, nc, depth + 1)) return False while lo < hi: mid = (lo + hi) // 2 if ok(mid): lo = mid + 1 else: hi = mid - 1 return lo - 1 print(bs(0, n ** 2))
#Verdict Execution timeMemoryGrader output
Fetching results...