# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
626060 | 54skyxenon | Mecho (IOI09_mecho) | Cpython 3 | 1099 ms | 17120 KiB |
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/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):
if not ok(lo):
return -1
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 - lo + 1) // 2
if ok(mid):
lo = mid
else:
hi = mid - 1
return lo
print(bs(0, n ** 2))
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |