Submission #626037

# Submission time Handle Problem Language Result Execution time Memory
626037 2022-08-11T07:07:42 Z 54skyxenon Mecho (IOI09_mecho) Python 3
21 / 100
1000 ms 24456 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))

def bs(lo, hi):
    def ok(mid):
        dist_hive = [[float('inf')] * n for _ in range(n)]
        # dist_mecho = [[float('inf')] * n for _ in range(n)]
        # dist_mecho[start[0]][start[1]] = mid

        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] in {'G', 'M'} and dist_hive[nr][nc] == float('inf'):
                    hives_Q.append((nr, nc, depth + 1))
                    dist_hive[nr][nc] = depth + 1

        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:
                # print('mid =', mid, 'is good!')
                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 (mid + ceil((depth + 1) / s)) < dist_hive[nr][nc] and not seen[nr][nc]:
                    seen[nr][nc] = True
                    mecho_Q.append((nr, nc, depth + 1))

        # print('at mid =', mid)
        # print('dist_hive:')
        # print(*dist_hive, sep='\n')
        # print('dist_mecho:')
        # print(*dist_mecho, sep='\n')
        # print('mid =', mid, 'is bad!')
        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 16 ms 3284 KB Output isn't correct
2 Incorrect 17 ms 3280 KB Output isn't correct
3 Incorrect 21 ms 3284 KB Output isn't correct
4 Incorrect 17 ms 3320 KB Output isn't correct
5 Incorrect 16 ms 3308 KB Output isn't correct
6 Correct 22 ms 3400 KB Output is correct
7 Execution timed out 1091 ms 10652 KB Time limit exceeded
8 Correct 25 ms 3412 KB Output is correct
9 Correct 27 ms 3400 KB Output is correct
10 Correct 28 ms 3356 KB Output is correct
11 Correct 23 ms 3384 KB Output is correct
12 Incorrect 110 ms 3464 KB Output isn't correct
13 Incorrect 81 ms 3428 KB Output isn't correct
14 Correct 139 ms 3452 KB Output is correct
15 Incorrect 25 ms 3284 KB Output isn't correct
16 Correct 23 ms 3324 KB Output is correct
17 Incorrect 26 ms 3396 KB Output isn't correct
18 Correct 25 ms 3316 KB Output is correct
19 Incorrect 32 ms 3420 KB Output isn't correct
20 Correct 34 ms 3412 KB Output is correct
21 Incorrect 48 ms 3420 KB Output isn't correct
22 Correct 47 ms 3412 KB Output is correct
23 Incorrect 59 ms 3476 KB Output isn't correct
24 Correct 75 ms 3444 KB Output is correct
25 Incorrect 106 ms 3504 KB Output isn't correct
26 Correct 97 ms 3444 KB Output is correct
27 Incorrect 101 ms 3504 KB Output isn't correct
28 Correct 124 ms 3460 KB Output is correct
29 Incorrect 122 ms 3592 KB Output isn't correct
30 Correct 137 ms 3484 KB Output is correct
31 Incorrect 161 ms 3448 KB Output isn't correct
32 Correct 153 ms 3448 KB Output is correct
33 Execution timed out 1085 ms 7432 KB Time limit exceeded
34 Execution timed out 1087 ms 7612 KB Time limit exceeded
35 Execution timed out 1090 ms 6644 KB Time limit exceeded
36 Execution timed out 1095 ms 8720 KB Time limit exceeded
37 Execution timed out 1047 ms 8640 KB Time limit exceeded
38 Execution timed out 1092 ms 8120 KB Time limit exceeded
39 Execution timed out 1089 ms 10180 KB Time limit exceeded
40 Execution timed out 1055 ms 10060 KB Time limit exceeded
41 Execution timed out 1094 ms 9796 KB Time limit exceeded
42 Execution timed out 1093 ms 11756 KB Time limit exceeded
43 Execution timed out 1085 ms 11580 KB Time limit exceeded
44 Execution timed out 1092 ms 11520 KB Time limit exceeded
45 Execution timed out 1088 ms 13344 KB Time limit exceeded
46 Execution timed out 1065 ms 13468 KB Time limit exceeded
47 Execution timed out 1089 ms 13808 KB Time limit exceeded
48 Execution timed out 1080 ms 15292 KB Time limit exceeded
49 Execution timed out 1094 ms 15208 KB Time limit exceeded
50 Execution timed out 1081 ms 16112 KB Time limit exceeded
51 Execution timed out 1092 ms 17208 KB Time limit exceeded
52 Execution timed out 1090 ms 17240 KB Time limit exceeded
53 Execution timed out 1082 ms 18728 KB Time limit exceeded
54 Execution timed out 1098 ms 19384 KB Time limit exceeded
55 Execution timed out 1089 ms 19464 KB Time limit exceeded
56 Execution timed out 1087 ms 16256 KB Time limit exceeded
57 Execution timed out 1089 ms 21884 KB Time limit exceeded
58 Execution timed out 1080 ms 21868 KB Time limit exceeded
59 Execution timed out 1088 ms 16624 KB Time limit exceeded
60 Execution timed out 1091 ms 24456 KB Time limit exceeded
61 Execution timed out 1100 ms 24340 KB Time limit exceeded
62 Execution timed out 1086 ms 16916 KB Time limit exceeded
63 Execution timed out 1097 ms 19868 KB Time limit exceeded
64 Execution timed out 1093 ms 20028 KB Time limit exceeded
65 Execution timed out 1091 ms 19420 KB Time limit exceeded
66 Execution timed out 1091 ms 19160 KB Time limit exceeded
67 Execution timed out 1084 ms 19076 KB Time limit exceeded
68 Execution timed out 1089 ms 9632 KB Time limit exceeded
69 Execution timed out 1093 ms 9592 KB Time limit exceeded
70 Execution timed out 1067 ms 9544 KB Time limit exceeded
71 Execution timed out 1082 ms 9584 KB Time limit exceeded
72 Execution timed out 1074 ms 9548 KB Time limit exceeded
73 Execution timed out 1091 ms 13292 KB Time limit exceeded
74 Execution timed out 1094 ms 13232 KB Time limit exceeded
75 Execution timed out 1086 ms 13200 KB Time limit exceeded
76 Execution timed out 1088 ms 13224 KB Time limit exceeded
77 Execution timed out 1060 ms 13240 KB Time limit exceeded
78 Execution timed out 1087 ms 12252 KB Time limit exceeded
79 Execution timed out 1090 ms 12268 KB Time limit exceeded
80 Execution timed out 1090 ms 12200 KB Time limit exceeded
81 Execution timed out 1093 ms 12296 KB Time limit exceeded
82 Execution timed out 1048 ms 12264 KB Time limit exceeded
83 Execution timed out 1088 ms 12052 KB Time limit exceeded
84 Execution timed out 1081 ms 12012 KB Time limit exceeded
85 Execution timed out 1084 ms 12108 KB Time limit exceeded
86 Execution timed out 1090 ms 12184 KB Time limit exceeded
87 Execution timed out 1085 ms 12000 KB Time limit exceeded
88 Execution timed out 1065 ms 11376 KB Time limit exceeded
89 Execution timed out 1081 ms 11304 KB Time limit exceeded
90 Execution timed out 1069 ms 11404 KB Time limit exceeded
91 Execution timed out 1082 ms 11416 KB Time limit exceeded
92 Execution timed out 1082 ms 11472 KB Time limit exceeded