Submission #629666

# Submission time Handle Problem Language Result Execution time Memory
629666 2022-08-14T21:22:36 Z 54skyxenon Mecho (IOI09_mecho) Python 3
46 / 100
1000 ms 35584 KB
# https://oj.uz/problem/view/IOI09_mecho
 
from collections import deque
 
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 valid_square(x, y):
	return 0 <= x < n and 0 <= y < n and (forest[x][y] == 'G' or forest[x][y] == 'M')

def mecho_reached(mecho_dis, bees_dis):
    return mecho_dis // s < bees_dis

def ok(eating_time):
    bees_visited = [[False] * n for _ in range(n)]
    mecho_visited = [[False] * n for _ in range(n)]
    bees_time = [[0] * n for _ in range(n)]
    mecho_time = [[0] * n for _ in range(n)]

    Q = deque()

    # move bees
    for hive_x, hive_y in hives:
        Q.append((hive_x, hive_y))
        bees_visited[hive_x][hive_y] = True

    while Q:
        x, y = Q.popleft()

        for nx, ny in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
            if valid_square(nx, ny) and not bees_visited[nx][ny]:
                bees_time[nx][ny] = bees_time[x][y] + 1    
                Q.append((nx, ny))
                bees_visited[nx][ny] = True
    
    # move Mecho
    Q.append(start)
    mecho_visited[start[0]][start[1]] = True
    if bees_time[start[0]][start[1]] <= eating_time:
        Q.popleft()
    
    while Q:
        x, y = Q.popleft()

        for nx, ny in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
            '''
            check if mecho reaces node[x][y] before the bees by dividing the time mecho takes to reach a node by s, since mecho walks s steps at a time.
            subtract the eating time from the time taken for the bees to reach the node, because that time was used by mecho for eating
            '''
            if valid_square(nx, ny) and not mecho_visited[nx][ny] and mecho_reached(mecho_time[x][y] + 1, bees_time[nx][ny] - eating_time):
                mecho_visited[nx][ny] = True
                Q.append((nx, ny))
                mecho_time[nx][ny] = mecho_time[x][y] + 1

    # check if mecho reached a node surrounding his cave before the bees
    for nx, ny in [(end[0] + 1, end[1]), (end[0] - 1, end[1]), (end[0], end[1] + 1), (end[0], end[1] - 1)]:
        if valid_square(nx, ny) and mecho_reached(mecho_time[nx][ny], bees_time[nx][ny] - eating_time) and mecho_visited[nx][ny]:
            return True

    return False
 
def bs(lo, hi):
    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 time Memory Grader output
1 Correct 16 ms 3412 KB Output is correct
2 Correct 16 ms 3412 KB Output is correct
3 Correct 16 ms 3476 KB Output is correct
4 Correct 16 ms 3332 KB Output is correct
5 Correct 16 ms 3404 KB Output is correct
6 Correct 18 ms 3428 KB Output is correct
7 Execution timed out 1076 ms 26516 KB Time limit exceeded
8 Correct 19 ms 3412 KB Output is correct
9 Correct 21 ms 3384 KB Output is correct
10 Correct 20 ms 3452 KB Output is correct
11 Correct 20 ms 3456 KB Output is correct
12 Correct 69 ms 3644 KB Output is correct
13 Correct 73 ms 3540 KB Output is correct
14 Correct 117 ms 3540 KB Output is correct
15 Correct 21 ms 3476 KB Output is correct
16 Correct 21 ms 3412 KB Output is correct
17 Correct 24 ms 3480 KB Output is correct
18 Correct 25 ms 3484 KB Output is correct
19 Correct 29 ms 3500 KB Output is correct
20 Correct 27 ms 3412 KB Output is correct
21 Correct 41 ms 3532 KB Output is correct
22 Correct 41 ms 3452 KB Output is correct
23 Correct 53 ms 3540 KB Output is correct
24 Correct 53 ms 3444 KB Output is correct
25 Correct 76 ms 3628 KB Output is correct
26 Correct 78 ms 3588 KB Output is correct
27 Correct 85 ms 3576 KB Output is correct
28 Correct 92 ms 3540 KB Output is correct
29 Correct 94 ms 3532 KB Output is correct
30 Correct 104 ms 3640 KB Output is correct
31 Correct 107 ms 3680 KB Output is correct
32 Correct 126 ms 3668 KB Output is correct
33 Execution timed out 1092 ms 10060 KB Time limit exceeded
34 Execution timed out 1090 ms 10380 KB Time limit exceeded
35 Execution timed out 1089 ms 8908 KB Time limit exceeded
36 Execution timed out 1092 ms 12060 KB Time limit exceeded
37 Execution timed out 1093 ms 12596 KB Time limit exceeded
38 Execution timed out 1078 ms 11272 KB Time limit exceeded
39 Execution timed out 1079 ms 14360 KB Time limit exceeded
40 Execution timed out 1082 ms 15116 KB Time limit exceeded
41 Execution timed out 1082 ms 13144 KB Time limit exceeded
42 Execution timed out 1077 ms 16980 KB Time limit exceeded
43 Execution timed out 1084 ms 16624 KB Time limit exceeded
44 Execution timed out 1082 ms 15844 KB Time limit exceeded
45 Execution timed out 1093 ms 19576 KB Time limit exceeded
46 Execution timed out 1084 ms 18448 KB Time limit exceeded
47 Execution timed out 1081 ms 18728 KB Time limit exceeded
48 Execution timed out 1083 ms 22088 KB Time limit exceeded
49 Execution timed out 1095 ms 21228 KB Time limit exceeded
50 Execution timed out 1093 ms 22040 KB Time limit exceeded
51 Execution timed out 1094 ms 24392 KB Time limit exceeded
52 Execution timed out 1087 ms 24424 KB Time limit exceeded
53 Execution timed out 1079 ms 25864 KB Time limit exceeded
54 Execution timed out 1080 ms 27760 KB Time limit exceeded
55 Execution timed out 1079 ms 27780 KB Time limit exceeded
56 Execution timed out 1082 ms 29424 KB Time limit exceeded
57 Execution timed out 1087 ms 31328 KB Time limit exceeded
58 Execution timed out 1091 ms 31304 KB Time limit exceeded
59 Execution timed out 1091 ms 30960 KB Time limit exceeded
60 Execution timed out 1093 ms 35176 KB Time limit exceeded
61 Execution timed out 1082 ms 35312 KB Time limit exceeded
62 Execution timed out 1082 ms 33848 KB Time limit exceeded
63 Execution timed out 1085 ms 35584 KB Time limit exceeded
64 Execution timed out 1094 ms 35404 KB Time limit exceeded
65 Execution timed out 1075 ms 34836 KB Time limit exceeded
66 Execution timed out 1089 ms 34740 KB Time limit exceeded
67 Execution timed out 1084 ms 35140 KB Time limit exceeded
68 Execution timed out 1082 ms 25624 KB Time limit exceeded
69 Execution timed out 1082 ms 25412 KB Time limit exceeded
70 Execution timed out 1081 ms 25484 KB Time limit exceeded
71 Execution timed out 1081 ms 25460 KB Time limit exceeded
72 Execution timed out 1079 ms 25464 KB Time limit exceeded
73 Execution timed out 1086 ms 29080 KB Time limit exceeded
74 Execution timed out 1091 ms 29072 KB Time limit exceeded
75 Execution timed out 1080 ms 29172 KB Time limit exceeded
76 Execution timed out 1093 ms 29084 KB Time limit exceeded
77 Execution timed out 1069 ms 29060 KB Time limit exceeded
78 Execution timed out 1089 ms 28160 KB Time limit exceeded
79 Execution timed out 1080 ms 28404 KB Time limit exceeded
80 Execution timed out 1085 ms 28348 KB Time limit exceeded
81 Execution timed out 1089 ms 28144 KB Time limit exceeded
82 Execution timed out 1085 ms 28164 KB Time limit exceeded
83 Execution timed out 1080 ms 28000 KB Time limit exceeded
84 Execution timed out 1084 ms 28100 KB Time limit exceeded
85 Execution timed out 1087 ms 28036 KB Time limit exceeded
86 Execution timed out 1085 ms 28016 KB Time limit exceeded
87 Execution timed out 1055 ms 27948 KB Time limit exceeded
88 Execution timed out 1048 ms 27276 KB Time limit exceeded
89 Execution timed out 1090 ms 27340 KB Time limit exceeded
90 Execution timed out 1088 ms 27248 KB Time limit exceeded
91 Execution timed out 1083 ms 27508 KB Time limit exceeded
92 Execution timed out 1087 ms 27192 KB Time limit exceeded