Submission #1020302

# Submission time Handle Problem Language Result Execution time Memory
1020302 2024-07-11T20:27:20 Z niwrad Mecho (IOI09_mecho) C++17
100 / 100
146 ms 8732 KB
#include <bits/stdc++.h>
using namespace std;


int r_c[]{0, 1, 0, -1};
int c_c[]{1, 0, -1, 0};
int n, s;
vector<vector<int>> arr;
int r_s, c_s;
int r_f, c_f;
queue<pair<int, int>> hives;
vector<vector<int>> dis;


void fill() {
    while (hives.size() != 0) {
        pair<int, int> t = hives.front();
        for (int i = 0; i < 4; i++) {
            int n_r = t.first + r_c[i];
            int n_c = t.second + c_c[i];
            if (min(n_r, n_c) >= 0 && max(n_r, n_c) < n) {
                if (arr[n_r][n_c] == 0 && dis[n_r][n_c] == -1) {
                    dis[n_r][n_c] = dis[t.first][t.second] + 1;
                    hives.push({n_r, n_c});
                }
            }
        }
        hives.pop();
    }
}

bool check(int time) {
    vector<vector<int>> len(n, vector<int>(n));
    queue<pair<int, int>> q;
    q.push({r_s, c_s});
    while (q.size() != 0) {
        pair<int, int> t = q.front();
        for (int i = 0; i < 4; i++) {
            int n_r = t.first + r_c[i];
            int n_c = t.second + c_c[i];
            if (min(n_r, n_c) >= 0 && max(n_r, n_c) < n) {
                if (arr[n_r][n_c] != -1 && len[n_r][n_c] == 0) {
                    if (dis[n_r][n_c] == -1 || (1 + len[t.first][t.second]) / s + time < dis[n_r][n_c]) {
                        len[n_r][n_c] = 1 + len[t.first][t.second];
                        q.push({n_r, n_c});
                    }
                }
            }
        }
        q.pop();
    }
    if (len[r_f][c_f] != 0) {
        return true;
    } else {
        return false;
    }
}




int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> s;
    arr.resize(n, vector<int>(n));
    dis.resize(n, vector<int>(n, -1));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            char c;
            cin >> c;
            if (c == 'T') {
                arr[i][j] = -1;
            } else if (c == 'M') {
                r_s = i; c_s = j;
            } else if (c == 'D') {
                arr[i][j] = 1;
                r_f = i; c_f = j;
            } else if (c == 'H') {
                dis[i][j] = 0;
                hives.push({i, j});
            }
        }
    }
    fill();
    if (!check(0)) {
        cout << "-1\n";
    } else {
        int l = 0;
        int r = dis[r_s][c_s];
        while (r > l + 1) {
            int m = (l + r) / 2;
            if (check(m)) {
                l = m;
            } else {
                r = m;
            }
        }
        cout << l << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 64 ms 8704 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 484 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 6 ms 2056 KB Output is correct
34 Correct 6 ms 2044 KB Output is correct
35 Correct 19 ms 2044 KB Output is correct
36 Correct 8 ms 2392 KB Output is correct
37 Correct 8 ms 2544 KB Output is correct
38 Correct 22 ms 2396 KB Output is correct
39 Correct 11 ms 3032 KB Output is correct
40 Correct 13 ms 3368 KB Output is correct
41 Correct 28 ms 3100 KB Output is correct
42 Correct 13 ms 3712 KB Output is correct
43 Correct 13 ms 3736 KB Output is correct
44 Correct 35 ms 3860 KB Output is correct
45 Correct 15 ms 4348 KB Output is correct
46 Correct 16 ms 4348 KB Output is correct
47 Correct 42 ms 4372 KB Output is correct
48 Correct 18 ms 5152 KB Output is correct
49 Correct 19 ms 5124 KB Output is correct
50 Correct 47 ms 5116 KB Output is correct
51 Correct 21 ms 5884 KB Output is correct
52 Correct 22 ms 5792 KB Output is correct
53 Correct 61 ms 5872 KB Output is correct
54 Correct 24 ms 6816 KB Output is correct
55 Correct 25 ms 6816 KB Output is correct
56 Correct 71 ms 6756 KB Output is correct
57 Correct 28 ms 7716 KB Output is correct
58 Correct 29 ms 7716 KB Output is correct
59 Correct 87 ms 7668 KB Output is correct
60 Correct 32 ms 8728 KB Output is correct
61 Correct 33 ms 8724 KB Output is correct
62 Correct 97 ms 8680 KB Output is correct
63 Correct 95 ms 8692 KB Output is correct
64 Correct 146 ms 8700 KB Output is correct
65 Correct 144 ms 8700 KB Output is correct
66 Correct 123 ms 8668 KB Output is correct
67 Correct 31 ms 8468 KB Output is correct
68 Correct 49 ms 8720 KB Output is correct
69 Correct 45 ms 8700 KB Output is correct
70 Correct 46 ms 8716 KB Output is correct
71 Correct 40 ms 8692 KB Output is correct
72 Correct 40 ms 8720 KB Output is correct
73 Correct 30 ms 8584 KB Output is correct
74 Correct 83 ms 8704 KB Output is correct
75 Correct 56 ms 8724 KB Output is correct
76 Correct 53 ms 8712 KB Output is correct
77 Correct 58 ms 8712 KB Output is correct
78 Correct 24 ms 8452 KB Output is correct
79 Correct 58 ms 8664 KB Output is correct
80 Correct 50 ms 8716 KB Output is correct
81 Correct 62 ms 8732 KB Output is correct
82 Correct 55 ms 8696 KB Output is correct
83 Correct 59 ms 8720 KB Output is correct
84 Correct 62 ms 8684 KB Output is correct
85 Correct 58 ms 8700 KB Output is correct
86 Correct 65 ms 8700 KB Output is correct
87 Correct 59 ms 8704 KB Output is correct
88 Correct 78 ms 8716 KB Output is correct
89 Correct 70 ms 8680 KB Output is correct
90 Correct 72 ms 8700 KB Output is correct
91 Correct 69 ms 8684 KB Output is correct
92 Correct 72 ms 8724 KB Output is correct