Submission #961890

# Submission time Handle Problem Language Result Execution time Memory
961890 2024-04-12T16:56:42 Z Ariadna Mecho (IOI09_mecho) C++14
84 / 100
138 ms 6816 KB
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

const int INF = 1e6;

vector<vector<int>> time_bees;

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;

        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 (pos != r) {
            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));
    map[home.first][home.second] = 'G';
    bfs_bees(hives, n, map);

    int l = 0, r = time_bees[home.first][home.second]+1, 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 1 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 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 61 ms 6248 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 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 Incorrect 0 ms 348 KB Output isn't correct
13 Incorrect 1 ms 344 KB Output isn't correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 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 1 ms 500 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 436 KB Output is correct
29 Correct 1 ms 600 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 8 ms 1628 KB Output is correct
34 Correct 7 ms 1628 KB Output is correct
35 Correct 9 ms 1628 KB Output is correct
36 Correct 10 ms 1960 KB Output is correct
37 Correct 9 ms 1884 KB Output is correct
38 Correct 12 ms 1884 KB Output is correct
39 Correct 13 ms 2364 KB Output is correct
40 Correct 12 ms 2420 KB Output is correct
41 Correct 15 ms 2240 KB Output is correct
42 Correct 23 ms 2860 KB Output is correct
43 Correct 16 ms 2864 KB Output is correct
44 Correct 18 ms 2648 KB Output is correct
45 Correct 19 ms 3348 KB Output is correct
46 Correct 18 ms 3340 KB Output is correct
47 Correct 22 ms 3276 KB Output is correct
48 Correct 29 ms 3912 KB Output is correct
49 Correct 21 ms 3884 KB Output is correct
50 Correct 26 ms 3816 KB Output is correct
51 Correct 27 ms 4432 KB Output is correct
52 Correct 27 ms 4444 KB Output is correct
53 Correct 33 ms 4408 KB Output is correct
54 Correct 30 ms 5152 KB Output is correct
55 Correct 31 ms 5204 KB Output is correct
56 Correct 37 ms 4952 KB Output is correct
57 Correct 37 ms 5820 KB Output is correct
58 Correct 36 ms 5928 KB Output is correct
59 Correct 40 ms 5824 KB Output is correct
60 Correct 40 ms 6616 KB Output is correct
61 Correct 41 ms 6816 KB Output is correct
62 Correct 49 ms 6576 KB Output is correct
63 Correct 98 ms 6536 KB Output is correct
64 Correct 138 ms 6536 KB Output is correct
65 Correct 133 ms 6620 KB Output is correct
66 Correct 110 ms 6552 KB Output is correct
67 Correct 104 ms 6564 KB Output is correct
68 Correct 58 ms 6600 KB Output is correct
69 Correct 47 ms 6552 KB Output is correct
70 Correct 56 ms 6644 KB Output is correct
71 Correct 52 ms 6804 KB Output is correct
72 Incorrect 43 ms 6544 KB Output isn't correct
73 Incorrect 42 ms 6564 KB Output isn't correct
74 Correct 61 ms 6604 KB Output is correct
75 Correct 57 ms 6648 KB Output is correct
76 Correct 55 ms 6580 KB Output is correct
77 Correct 57 ms 6624 KB Output is correct
78 Correct 59 ms 6744 KB Output is correct
79 Correct 54 ms 6640 KB Output is correct
80 Correct 57 ms 6528 KB Output is correct
81 Correct 66 ms 6548 KB Output is correct
82 Correct 62 ms 6760 KB Output is correct
83 Correct 61 ms 6588 KB Output is correct
84 Correct 58 ms 6440 KB Output is correct
85 Correct 60 ms 6564 KB Output is correct
86 Correct 58 ms 6596 KB Output is correct
87 Correct 55 ms 6612 KB Output is correct
88 Correct 75 ms 6608 KB Output is correct
89 Correct 57 ms 6484 KB Output is correct
90 Correct 62 ms 6612 KB Output is correct
91 Correct 58 ms 6612 KB Output is correct
92 Correct 59 ms 6612 KB Output is correct