Submission #629674

# Submission time Handle Problem Language Result Execution time Memory
629674 2022-08-14T21:37:47 Z 54skyxenon Mecho (IOI09_mecho) Python 3
46 / 100
1000 ms 27616 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')

bees_time = [[0] * n for _ in range(n)]
bees_visited = [[False] * 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

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

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

    Q = deque()

    # 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):
    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
 
print(bs(0, n ** 2))
# Verdict Execution time Memory Grader output
1 Correct 17 ms 3412 KB Output is correct
2 Correct 17 ms 3412 KB Output is correct
3 Correct 17 ms 3404 KB Output is correct
4 Correct 17 ms 3412 KB Output is correct
5 Correct 29 ms 3448 KB Output is correct
6 Correct 27 ms 3404 KB Output is correct
7 Execution timed out 1076 ms 15700 KB Time limit exceeded
8 Correct 19 ms 3472 KB Output is correct
9 Correct 22 ms 3420 KB Output is correct
10 Correct 23 ms 3416 KB Output is correct
11 Correct 19 ms 3440 KB Output is correct
12 Correct 28 ms 3496 KB Output is correct
13 Correct 33 ms 3452 KB Output is correct
14 Correct 43 ms 3572 KB Output is correct
15 Correct 20 ms 3412 KB Output is correct
16 Correct 25 ms 3384 KB Output is correct
17 Correct 30 ms 3388 KB Output is correct
18 Correct 26 ms 3412 KB Output is correct
19 Correct 30 ms 3384 KB Output is correct
20 Correct 31 ms 3392 KB Output is correct
21 Correct 25 ms 3540 KB Output is correct
22 Correct 26 ms 3456 KB Output is correct
23 Correct 30 ms 3444 KB Output is correct
24 Correct 34 ms 3500 KB Output is correct
25 Correct 37 ms 3516 KB Output is correct
26 Correct 45 ms 3584 KB Output is correct
27 Correct 39 ms 3608 KB Output is correct
28 Correct 53 ms 3552 KB Output is correct
29 Correct 39 ms 3532 KB Output is correct
30 Correct 52 ms 3540 KB Output is correct
31 Correct 45 ms 3680 KB Output is correct
32 Correct 66 ms 3592 KB Output is correct
33 Correct 560 ms 9880 KB Output is correct
34 Correct 696 ms 10316 KB Output is correct
35 Execution timed out 1100 ms 10924 KB Time limit exceeded
36 Correct 714 ms 11836 KB Output is correct
37 Correct 994 ms 12400 KB Output is correct
38 Execution timed out 1090 ms 13480 KB Time limit exceeded
39 Correct 910 ms 14196 KB Output is correct
40 Execution timed out 1091 ms 14812 KB Time limit exceeded
41 Execution timed out 1072 ms 16196 KB Time limit exceeded
42 Execution timed out 1089 ms 16608 KB Time limit exceeded
43 Execution timed out 1093 ms 17568 KB Time limit exceeded
44 Execution timed out 1099 ms 18216 KB Time limit exceeded
45 Execution timed out 1091 ms 19400 KB Time limit exceeded
46 Execution timed out 1098 ms 20552 KB Time limit exceeded
47 Execution timed out 1097 ms 18616 KB Time limit exceeded
48 Execution timed out 1087 ms 22352 KB Time limit exceeded
49 Execution timed out 1097 ms 23808 KB Time limit exceeded
50 Execution timed out 1088 ms 15964 KB Time limit exceeded
51 Execution timed out 1094 ms 25648 KB Time limit exceeded
52 Execution timed out 1094 ms 24928 KB Time limit exceeded
53 Execution timed out 1090 ms 16592 KB Time limit exceeded
54 Execution timed out 1079 ms 27616 KB Time limit exceeded
55 Execution timed out 1088 ms 20700 KB Time limit exceeded
56 Execution timed out 1097 ms 17876 KB Time limit exceeded
57 Execution timed out 1094 ms 21164 KB Time limit exceeded
58 Execution timed out 1096 ms 21124 KB Time limit exceeded
59 Execution timed out 1085 ms 19000 KB Time limit exceeded
60 Execution timed out 1090 ms 22188 KB Time limit exceeded
61 Execution timed out 1088 ms 21748 KB Time limit exceeded
62 Execution timed out 1092 ms 19808 KB Time limit exceeded
63 Execution timed out 1088 ms 21448 KB Time limit exceeded
64 Execution timed out 1085 ms 21184 KB Time limit exceeded
65 Execution timed out 1091 ms 21348 KB Time limit exceeded
66 Execution timed out 1090 ms 20980 KB Time limit exceeded
67 Execution timed out 1096 ms 21044 KB Time limit exceeded
68 Execution timed out 1079 ms 14688 KB Time limit exceeded
69 Execution timed out 1065 ms 14620 KB Time limit exceeded
70 Execution timed out 1089 ms 14684 KB Time limit exceeded
71 Execution timed out 1089 ms 14628 KB Time limit exceeded
72 Execution timed out 1084 ms 14860 KB Time limit exceeded
73 Execution timed out 1081 ms 18360 KB Time limit exceeded
74 Execution timed out 1081 ms 18380 KB Time limit exceeded
75 Execution timed out 1084 ms 18336 KB Time limit exceeded
76 Execution timed out 1091 ms 18288 KB Time limit exceeded
77 Execution timed out 1085 ms 18328 KB Time limit exceeded
78 Execution timed out 1087 ms 17284 KB Time limit exceeded
79 Execution timed out 1058 ms 17256 KB Time limit exceeded
80 Execution timed out 1060 ms 17368 KB Time limit exceeded
81 Execution timed out 1082 ms 17628 KB Time limit exceeded
82 Execution timed out 1090 ms 17336 KB Time limit exceeded
83 Execution timed out 1088 ms 17252 KB Time limit exceeded
84 Execution timed out 1085 ms 17172 KB Time limit exceeded
85 Execution timed out 1094 ms 17152 KB Time limit exceeded
86 Execution timed out 1086 ms 17288 KB Time limit exceeded
87 Execution timed out 1091 ms 17184 KB Time limit exceeded
88 Execution timed out 1090 ms 16432 KB Time limit exceeded
89 Execution timed out 1090 ms 16492 KB Time limit exceeded
90 Execution timed out 1090 ms 16708 KB Time limit exceeded
91 Execution timed out 1092 ms 16412 KB Time limit exceeded
92 Execution timed out 1098 ms 16524 KB Time limit exceeded