Submission #626054

# Submission time Handle Problem Language Result Execution time Memory
626054 2022-08-11T07:33:13 Z 54skyxenon Mecho (IOI09_mecho) Python 3
21 / 100
1000 ms 19712 KB
# 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 + ceil((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

    if lo > hi:
        return -1

    mid = (lo + hi) // 2

    if not ok(mid):
        return bs(lo, mid - 1)
    elif mid < hi and ok(mid + 1):
        return bs(mid + 1, hi)
    else:
        return mid + 1

print(bs(0, n ** 2))
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 3280 KB Output isn't correct
2 Incorrect 22 ms 3316 KB Output isn't correct
3 Incorrect 18 ms 3332 KB Output isn't correct
4 Incorrect 25 ms 3256 KB Output isn't correct
5 Incorrect 17 ms 3288 KB Output isn't correct
6 Correct 19 ms 3396 KB Output is correct
7 Execution timed out 1086 ms 10732 KB Time limit exceeded
8 Correct 17 ms 3328 KB Output is correct
9 Correct 21 ms 3284 KB Output is correct
10 Correct 17 ms 3284 KB Output is correct
11 Correct 20 ms 3328 KB Output is correct
12 Incorrect 26 ms 3432 KB Output isn't correct
13 Incorrect 27 ms 3408 KB Output isn't correct
14 Correct 50 ms 3456 KB Output is correct
15 Incorrect 23 ms 3384 KB Output isn't correct
16 Correct 23 ms 3392 KB Output is correct
17 Incorrect 20 ms 3388 KB Output isn't correct
18 Correct 22 ms 3332 KB Output is correct
19 Incorrect 18 ms 3412 KB Output isn't correct
20 Correct 20 ms 3412 KB Output is correct
21 Incorrect 19 ms 3396 KB Output isn't correct
22 Correct 22 ms 3412 KB Output is correct
23 Incorrect 21 ms 3376 KB Output isn't correct
24 Correct 24 ms 3420 KB Output is correct
25 Incorrect 25 ms 3520 KB Output isn't correct
26 Correct 29 ms 3412 KB Output is correct
27 Incorrect 26 ms 3440 KB Output isn't correct
28 Correct 33 ms 3412 KB Output is correct
29 Incorrect 29 ms 3392 KB Output isn't correct
30 Correct 36 ms 3512 KB Output is correct
31 Incorrect 30 ms 3488 KB Output isn't correct
32 Correct 36 ms 3524 KB Output is correct
33 Incorrect 313 ms 7504 KB Output isn't correct
34 Correct 335 ms 7396 KB Output is correct
35 Execution timed out 1085 ms 6716 KB Time limit exceeded
36 Incorrect 382 ms 8664 KB Output isn't correct
37 Correct 417 ms 8648 KB Output is correct
38 Execution timed out 1082 ms 8124 KB Time limit exceeded
39 Incorrect 493 ms 10156 KB Output isn't correct
40 Correct 508 ms 10192 KB Output is correct
41 Execution timed out 1098 ms 9872 KB Time limit exceeded
42 Incorrect 583 ms 11700 KB Output isn't correct
43 Correct 643 ms 11620 KB Output is correct
44 Execution timed out 1097 ms 11576 KB Time limit exceeded
45 Incorrect 686 ms 13396 KB Output isn't correct
46 Correct 779 ms 13352 KB Output is correct
47 Execution timed out 1072 ms 13848 KB Time limit exceeded
48 Incorrect 864 ms 15360 KB Output isn't correct
49 Correct 885 ms 15332 KB Output is correct
50 Execution timed out 1089 ms 12640 KB Time limit exceeded
51 Incorrect 953 ms 17344 KB Output isn't correct
52 Execution timed out 1040 ms 17368 KB Time limit exceeded
53 Execution timed out 1092 ms 13016 KB Time limit exceeded
54 Execution timed out 1097 ms 19560 KB Time limit exceeded
55 Execution timed out 1081 ms 19712 KB Time limit exceeded
56 Execution timed out 1087 ms 13648 KB Time limit exceeded
57 Execution timed out 1094 ms 17328 KB Time limit exceeded
58 Execution timed out 1087 ms 15936 KB Time limit exceeded
59 Execution timed out 1087 ms 14272 KB Time limit exceeded
60 Execution timed out 1092 ms 16976 KB Time limit exceeded
61 Execution timed out 1094 ms 17016 KB Time limit exceeded
62 Execution timed out 1087 ms 14964 KB Time limit exceeded
63 Execution timed out 1091 ms 16612 KB Time limit exceeded
64 Execution timed out 1087 ms 17060 KB Time limit exceeded
65 Execution timed out 1073 ms 16940 KB Time limit exceeded
66 Execution timed out 1078 ms 16076 KB Time limit exceeded
67 Execution timed out 1092 ms 16616 KB Time limit exceeded
68 Execution timed out 1085 ms 9772 KB Time limit exceeded
69 Execution timed out 1081 ms 9648 KB Time limit exceeded
70 Execution timed out 1094 ms 9528 KB Time limit exceeded
71 Execution timed out 1090 ms 9552 KB Time limit exceeded
72 Execution timed out 1078 ms 9572 KB Time limit exceeded
73 Execution timed out 1090 ms 13224 KB Time limit exceeded
74 Execution timed out 1055 ms 13156 KB Time limit exceeded
75 Execution timed out 1085 ms 13220 KB Time limit exceeded
76 Execution timed out 1093 ms 13136 KB Time limit exceeded
77 Execution timed out 1037 ms 13232 KB Time limit exceeded
78 Execution timed out 1052 ms 12288 KB Time limit exceeded
79 Execution timed out 1082 ms 12420 KB Time limit exceeded
80 Execution timed out 1077 ms 12272 KB Time limit exceeded
81 Execution timed out 1081 ms 12156 KB Time limit exceeded
82 Execution timed out 1094 ms 12276 KB Time limit exceeded
83 Execution timed out 1086 ms 12176 KB Time limit exceeded
84 Execution timed out 1094 ms 12120 KB Time limit exceeded
85 Execution timed out 1083 ms 12204 KB Time limit exceeded
86 Execution timed out 1060 ms 12108 KB Time limit exceeded
87 Execution timed out 1087 ms 12068 KB Time limit exceeded
88 Execution timed out 1085 ms 11500 KB Time limit exceeded
89 Execution timed out 1083 ms 11584 KB Time limit exceeded
90 Execution timed out 1093 ms 11344 KB Time limit exceeded
91 Execution timed out 1093 ms 11340 KB Time limit exceeded
92 Execution timed out 1093 ms 11400 KB Time limit exceeded