Submission #629672

# Submission time Handle Problem Language Result Execution time Memory
629672 2022-08-14T21:35:38 Z 54skyxenon Mecho (IOI09_mecho) Python 3
46 / 100
1000 ms 34512 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):
    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 19 ms 3452 KB Output is correct
2 Correct 19 ms 3360 KB Output is correct
3 Correct 22 ms 3404 KB Output is correct
4 Correct 23 ms 3368 KB Output is correct
5 Correct 23 ms 3456 KB Output is correct
6 Correct 23 ms 3340 KB Output is correct
7 Execution timed out 1082 ms 25856 KB Time limit exceeded
8 Correct 23 ms 3360 KB Output is correct
9 Correct 26 ms 3360 KB Output is correct
10 Correct 25 ms 3432 KB Output is correct
11 Correct 21 ms 3372 KB Output is correct
12 Correct 90 ms 3568 KB Output is correct
13 Correct 78 ms 3524 KB Output is correct
14 Correct 127 ms 3572 KB Output is correct
15 Correct 33 ms 3476 KB Output is correct
16 Correct 24 ms 3392 KB Output is correct
17 Correct 36 ms 3456 KB Output is correct
18 Correct 37 ms 3432 KB Output is correct
19 Correct 48 ms 3440 KB Output is correct
20 Correct 34 ms 3540 KB Output is correct
21 Correct 65 ms 3492 KB Output is correct
22 Correct 58 ms 3620 KB Output is correct
23 Correct 79 ms 3564 KB Output is correct
24 Correct 79 ms 3552 KB Output is correct
25 Correct 127 ms 3632 KB Output is correct
26 Correct 129 ms 3584 KB Output is correct
27 Correct 128 ms 3588 KB Output is correct
28 Correct 166 ms 3704 KB Output is correct
29 Correct 153 ms 3624 KB Output is correct
30 Correct 160 ms 3620 KB Output is correct
31 Correct 167 ms 3684 KB Output is correct
32 Correct 213 ms 3648 KB Output is correct
33 Execution timed out 1094 ms 9880 KB Time limit exceeded
34 Execution timed out 1091 ms 10396 KB Time limit exceeded
35 Execution timed out 1041 ms 8636 KB Time limit exceeded
36 Execution timed out 1091 ms 11840 KB Time limit exceeded
37 Execution timed out 1094 ms 12376 KB Time limit exceeded
38 Execution timed out 1094 ms 10776 KB Time limit exceeded
39 Execution timed out 1089 ms 14112 KB Time limit exceeded
40 Execution timed out 1093 ms 14936 KB Time limit exceeded
41 Execution timed out 1085 ms 13060 KB Time limit exceeded
42 Execution timed out 1076 ms 16732 KB Time limit exceeded
43 Execution timed out 1095 ms 15576 KB Time limit exceeded
44 Execution timed out 1089 ms 15580 KB Time limit exceeded
45 Execution timed out 1097 ms 19316 KB Time limit exceeded
46 Execution timed out 1085 ms 18232 KB Time limit exceeded
47 Execution timed out 1089 ms 18508 KB Time limit exceeded
48 Execution timed out 1094 ms 20928 KB Time limit exceeded
49 Execution timed out 1093 ms 20848 KB Time limit exceeded
50 Execution timed out 1099 ms 21716 KB Time limit exceeded
51 Execution timed out 1080 ms 23944 KB Time limit exceeded
52 Execution timed out 1084 ms 23960 KB Time limit exceeded
53 Execution timed out 1085 ms 25288 KB Time limit exceeded
54 Execution timed out 1099 ms 27332 KB Time limit exceeded
55 Execution timed out 1094 ms 27244 KB Time limit exceeded
56 Execution timed out 1096 ms 28552 KB Time limit exceeded
57 Execution timed out 1083 ms 30876 KB Time limit exceeded
58 Execution timed out 1098 ms 30808 KB Time limit exceeded
59 Execution timed out 1094 ms 30220 KB Time limit exceeded
60 Execution timed out 1070 ms 34512 KB Time limit exceeded
61 Execution timed out 1092 ms 34500 KB Time limit exceeded
62 Execution timed out 1093 ms 32148 KB Time limit exceeded
63 Execution timed out 1093 ms 33224 KB Time limit exceeded
64 Execution timed out 1091 ms 33972 KB Time limit exceeded
65 Execution timed out 1092 ms 34044 KB Time limit exceeded
66 Execution timed out 1078 ms 33840 KB Time limit exceeded
67 Execution timed out 1095 ms 32144 KB Time limit exceeded
68 Execution timed out 1090 ms 24796 KB Time limit exceeded
69 Execution timed out 1096 ms 24732 KB Time limit exceeded
70 Execution timed out 1087 ms 24864 KB Time limit exceeded
71 Execution timed out 1089 ms 24968 KB Time limit exceeded
72 Execution timed out 1095 ms 24840 KB Time limit exceeded
73 Execution timed out 1085 ms 28572 KB Time limit exceeded
74 Execution timed out 1081 ms 28492 KB Time limit exceeded
75 Execution timed out 1084 ms 28484 KB Time limit exceeded
76 Execution timed out 1086 ms 28428 KB Time limit exceeded
77 Execution timed out 1084 ms 28468 KB Time limit exceeded
78 Execution timed out 1092 ms 27596 KB Time limit exceeded
79 Execution timed out 1096 ms 27508 KB Time limit exceeded
80 Execution timed out 1087 ms 27420 KB Time limit exceeded
81 Execution timed out 1096 ms 27484 KB Time limit exceeded
82 Execution timed out 1077 ms 27516 KB Time limit exceeded
83 Execution timed out 1088 ms 27548 KB Time limit exceeded
84 Execution timed out 1096 ms 27256 KB Time limit exceeded
85 Execution timed out 1083 ms 27276 KB Time limit exceeded
86 Execution timed out 1088 ms 27332 KB Time limit exceeded
87 Execution timed out 1092 ms 27360 KB Time limit exceeded
88 Execution timed out 1077 ms 26860 KB Time limit exceeded
89 Execution timed out 1089 ms 26744 KB Time limit exceeded
90 Execution timed out 1091 ms 26748 KB Time limit exceeded
91 Execution timed out 1091 ms 26620 KB Time limit exceeded
92 Execution timed out 1093 ms 26620 KB Time limit exceeded