Submission #961895

# Submission time Handle Problem Language Result Execution time Memory
961895 2024-04-12T17:19:52 Z Ariadna Mecho (IOI09_mecho) C++14
100 / 100
151 ms 6312 KB
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

const int INF = 1e6;

vector<vector<int>> time_bees;
int max_bees = 0;
void bfs_bees(vector<pair<int, int>>& hives, int n, vector<vector<char>>& map) {
    queue<pair<int, int>> q;
    for (pair<int, int> h : hives) {
        q.push(h);
        time_bees[h.first][h.second] = 0;
    }

    while (!q.empty()) {
        pair<int, int> pos = q.front();
        q.pop();
        int i = pos.first, j = pos.second;
        max_bees = max(max_bees, time_bees[i][j]);

        if (i > 0 && time_bees[i-1][j] == INF && map[i-1][j] == 'G') {
            time_bees[i-1][j] = time_bees[i][j]+1;
            q.push({i-1, j});
        }
        if (i < n-1 && time_bees[i+1][j] == INF && map[i+1][j] == 'G') {
            time_bees[i+1][j] = time_bees[i][j]+1;
            q.push({i+1, j});
        }
        if (j > 0 && time_bees[i][j-1] == INF && map[i][j-1] == 'G') {
            time_bees[i][j-1] = time_bees[i][j]+1;
            q.push({i, j-1});
        }
        if (j < n-1 && time_bees[i][j+1] == INF && map[i][j+1] == 'G') {
            time_bees[i][j+1] = time_bees[i][j]+1;
            q.push({i, j+1});
        }
    }
}

bool CanGetHome(pair<int, int> r, vector<vector<char>>& map, int s, int t, pair<int, int> d) {
    int n = (int)map.size();
    queue<pair<pair<int, int>, int>> q;
    vector<vector<int>> visited(n, vector<int>(n, 0));
    visited[r.first][r.second] = 1;
    q.push({r, s*t});
    while (!q.empty()) {
        pair<int, int> pos = q.front().first;
        int steps = q.front().second;
        q.pop();
        int i = pos.first, j = pos.second;

        if (pos == d) return true;
        if (steps%s == 0) {
            if ((steps+s-1)/s >= time_bees[i][j]) continue;
        } else if ((steps+s-1)/s > time_bees[i][j]) continue;

        if (i > 0 && !visited[i-1][j] && map[i-1][j] == 'G') {
            visited[i-1][j] = 1;
            q.push({{i-1, j}, steps+1});
        }  
        if (i < n-1 && !visited[i+1][j] && map[i+1][j] == 'G') {
            visited[i+1][j] = 1;
            q.push({{i+1, j}, steps+1});
        } 
        if (j > 0 && !visited[i][j-1] && map[i][j-1] == 'G') {
            visited[i][j-1] = 1;
            q.push({{i, j-1}, steps+1});
        }
        if (j < n-1 && !visited[i][j+1] && map[i][j+1] == 'G') {
            visited[i][j+1] = 1;
            q.push({{i, j+1}, steps+1});
        }
    }
    return false;
}

int main() {
    int n, s;
    cin >> n >> s;
    vector<vector<char>> map(n, vector<char>(n));
    vector<pair<int, int>> hives;
    pair<int, int> mecho, home;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> map[i][j];
            if (map[i][j] == 'H') hives.pb({i, j});
            else if (map[i][j] == 'M') { 
                mecho = {i, j};
                map[i][j] = 'G';
            } else if (map[i][j] == 'D') home = {i, j};
        }
    }

    time_bees = vector<vector<int>>(n, vector<int>(n, INF));
    bfs_bees(hives, n, map);
    map[home.first][home.second] = 'G';

    int l = 0, r = max_bees, ans = -1;
    while (l <= r) {
        int m = (l+r)/2;
        if (CanGetHome(mecho, map, s, m, home)) {
            l = m+1;
            ans = m;
        } else r = m-1;
    }
    cout << ans << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 68 ms 6156 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 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 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 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 344 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 7 ms 1532 KB Output is correct
34 Correct 8 ms 1524 KB Output is correct
35 Correct 15 ms 1476 KB Output is correct
36 Correct 10 ms 1760 KB Output is correct
37 Correct 9 ms 1880 KB Output is correct
38 Correct 19 ms 1880 KB Output is correct
39 Correct 14 ms 2276 KB Output is correct
40 Correct 12 ms 2264 KB Output is correct
41 Correct 24 ms 2228 KB Output is correct
42 Correct 15 ms 2684 KB Output is correct
43 Correct 15 ms 2692 KB Output is correct
44 Correct 31 ms 2676 KB Output is correct
45 Correct 20 ms 3212 KB Output is correct
46 Correct 18 ms 3160 KB Output is correct
47 Correct 40 ms 3172 KB Output is correct
48 Correct 23 ms 3904 KB Output is correct
49 Correct 21 ms 3692 KB Output is correct
50 Correct 58 ms 3644 KB Output is correct
51 Correct 25 ms 4188 KB Output is correct
52 Correct 25 ms 4208 KB Output is correct
53 Correct 56 ms 4476 KB Output is correct
54 Correct 38 ms 4840 KB Output is correct
55 Correct 30 ms 4836 KB Output is correct
56 Correct 71 ms 4840 KB Output is correct
57 Correct 35 ms 5464 KB Output is correct
58 Correct 33 ms 5468 KB Output is correct
59 Correct 77 ms 5464 KB Output is correct
60 Correct 39 ms 6180 KB Output is correct
61 Correct 37 ms 6184 KB Output is correct
62 Correct 87 ms 6144 KB Output is correct
63 Correct 102 ms 6180 KB Output is correct
64 Correct 151 ms 6164 KB Output is correct
65 Correct 145 ms 6156 KB Output is correct
66 Correct 120 ms 6312 KB Output is correct
67 Correct 106 ms 6128 KB Output is correct
68 Correct 55 ms 6148 KB Output is correct
69 Correct 49 ms 6164 KB Output is correct
70 Correct 53 ms 6200 KB Output is correct
71 Correct 46 ms 6148 KB Output is correct
72 Correct 43 ms 6160 KB Output is correct
73 Correct 44 ms 6220 KB Output is correct
74 Correct 55 ms 6208 KB Output is correct
75 Correct 54 ms 6168 KB Output is correct
76 Correct 56 ms 6216 KB Output is correct
77 Correct 62 ms 6200 KB Output is correct
78 Correct 62 ms 6176 KB Output is correct
79 Correct 57 ms 6200 KB Output is correct
80 Correct 56 ms 6188 KB Output is correct
81 Correct 61 ms 6224 KB Output is correct
82 Correct 52 ms 6192 KB Output is correct
83 Correct 66 ms 6184 KB Output is correct
84 Correct 62 ms 6184 KB Output is correct
85 Correct 58 ms 6172 KB Output is correct
86 Correct 64 ms 6180 KB Output is correct
87 Correct 63 ms 6192 KB Output is correct
88 Correct 67 ms 6088 KB Output is correct
89 Correct 71 ms 6188 KB Output is correct
90 Correct 68 ms 6172 KB Output is correct
91 Correct 65 ms 6156 KB Output is correct
92 Correct 66 ms 6168 KB Output is correct