Submission #1034941

# Submission time Handle Problem Language Result Execution time Memory
1034941 2024-07-25T22:51:18 Z ArthuroWich Mecho (IOI09_mecho) C++17
100 / 100
130 ms 17400 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int n, s;
char grid[805][805];
int hivedist[805][805], mechodist[805][805], vis[805][805];
vector<pair<int, int>> delta = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}, hives;
pair<int, int> home, mecho;
bool valid(int i, int j) {
    return (i > 0 && j > 0 && i <= n && j <= n && (grid[i][j] == 'G' || grid[i][j] == 'H' || grid[i][j] == 'M'));
}
void hivebfs() {
    queue<pair<int, int>> q;
    for (auto c : hives) {
        q.push(c);
        hivedist[c.first][c.second] = 0;
    }
    while(!q.empty()) {
        auto [a, b] = q.front();
        q.pop();
        for (auto [dx, dy] : delta) {
            if (valid(a+dx, b+dy) && hivedist[a+dx][b+dy] > hivedist[a][b]+1) {
                hivedist[a+dx][b+dy] = hivedist[a][b]+1;
                q.push({a+dx, b+dy});
            }
        }
    }
}
bool check(int x) {
    if (hivedist[mecho.first][mecho.second] <= x) {
        return 0;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            mechodist[i][j] = INT_MAX;
            vis[i][j] = 0;
        }
    }
    queue<pair<int, int>> q;
    q.push(mecho);
    mechodist[mecho.first][mecho.second] = 0;
    vis[mecho.first][mecho.second] = 1;
    while(!q.empty()) {
        auto [a, b] = q.front();
        q.pop();
        for (auto [dx, dy] : delta) {
            if (valid(a+dx, b+dy) && !vis[a+dx][b+dy] && (mechodist[a][b]+1)/s+x < hivedist[a+dx][b+dy]) {
                vis[a+dx][b+dy] = 1;
                mechodist[a+dx][b+dy] = mechodist[a][b]+1;
                q.push({a+dx, b+dy});
            }
        }
    }
    auto [a, b] = home;
    for (auto [dx, dy] : delta) {
        if (valid(a+dx, b+dy) && vis[a+dx][b+dy]) {
            return 1;
        }
    }
    return 0;
}
void solve() {
    cin >> n >> s;
    for (int i = 1; i <= n; i++) {
        string s;
        cin >> s;
        for (int j = 1; j <= n; j++) {
            grid[i][j] = s[j-1];
            mechodist[i][j] = INT_MAX;
            hivedist[i][j] = INT_MAX;
            if (s[j-1] == 'H') {
                hives.push_back({i, j});
            } else if (s[j-1] == 'M') {
                mecho = {i, j};
            } else if (s[j-1] == 'D') {
                home = {i, j};
            }
        }
    }    
    hivebfs();
    int l = 0, r = 1e8;
    while(l < r) {
        int m = (l+r+1)/2;
        if (check(m)) {
            l = m;
        } else {
            r = m-1;
        }
    }
    if (check(l)) {
        cout << l << endl;
    } else {
        cout << -1 << endl;
    }
}
int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    t = 1;
    while(t--) {
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 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 1 ms 604 KB Output is correct
7 Correct 81 ms 16988 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 1 ms 1116 KB Output is correct
13 Correct 1 ms 1116 KB Output is correct
14 Correct 1 ms 1368 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 0 ms 860 KB Output is correct
20 Correct 0 ms 860 KB Output is correct
21 Correct 0 ms 860 KB Output is correct
22 Correct 1 ms 856 KB Output is correct
23 Correct 1 ms 1116 KB Output is correct
24 Correct 1 ms 1116 KB Output is correct
25 Correct 1 ms 1116 KB Output is correct
26 Correct 1 ms 1116 KB Output is correct
27 Correct 1 ms 1372 KB Output is correct
28 Correct 1 ms 1372 KB Output is correct
29 Correct 1 ms 1368 KB Output is correct
30 Correct 1 ms 1372 KB Output is correct
31 Correct 1 ms 1372 KB Output is correct
32 Correct 1 ms 1372 KB Output is correct
33 Correct 6 ms 7356 KB Output is correct
34 Correct 5 ms 7260 KB Output is correct
35 Correct 21 ms 7476 KB Output is correct
36 Correct 7 ms 8284 KB Output is correct
37 Correct 7 ms 8528 KB Output is correct
38 Correct 29 ms 8284 KB Output is correct
39 Correct 8 ms 9564 KB Output is correct
40 Correct 7 ms 9520 KB Output is correct
41 Correct 36 ms 9564 KB Output is correct
42 Correct 9 ms 10588 KB Output is correct
43 Correct 9 ms 10492 KB Output is correct
44 Correct 43 ms 10588 KB Output is correct
45 Correct 11 ms 11608 KB Output is correct
46 Correct 11 ms 11612 KB Output is correct
47 Correct 50 ms 11612 KB Output is correct
48 Correct 12 ms 12632 KB Output is correct
49 Correct 14 ms 12636 KB Output is correct
50 Correct 65 ms 12636 KB Output is correct
51 Correct 15 ms 13656 KB Output is correct
52 Correct 14 ms 13660 KB Output is correct
53 Correct 70 ms 13728 KB Output is correct
54 Correct 17 ms 14684 KB Output is correct
55 Correct 17 ms 14704 KB Output is correct
56 Correct 81 ms 14764 KB Output is correct
57 Correct 17 ms 15708 KB Output is correct
58 Correct 18 ms 15704 KB Output is correct
59 Correct 83 ms 15612 KB Output is correct
60 Correct 20 ms 16732 KB Output is correct
61 Correct 17 ms 16732 KB Output is correct
62 Correct 110 ms 16732 KB Output is correct
63 Correct 88 ms 16724 KB Output is correct
64 Correct 121 ms 16724 KB Output is correct
65 Correct 130 ms 16984 KB Output is correct
66 Correct 99 ms 16728 KB Output is correct
67 Correct 111 ms 16724 KB Output is correct
68 Correct 39 ms 16732 KB Output is correct
69 Correct 35 ms 16732 KB Output is correct
70 Correct 33 ms 16724 KB Output is correct
71 Correct 36 ms 16732 KB Output is correct
72 Correct 23 ms 16728 KB Output is correct
73 Correct 29 ms 17312 KB Output is correct
74 Correct 49 ms 17124 KB Output is correct
75 Correct 55 ms 17244 KB Output is correct
76 Correct 51 ms 17236 KB Output is correct
77 Correct 57 ms 17344 KB Output is correct
78 Correct 63 ms 17400 KB Output is correct
79 Correct 49 ms 17232 KB Output is correct
80 Correct 51 ms 17240 KB Output is correct
81 Correct 53 ms 17236 KB Output is correct
82 Correct 51 ms 17236 KB Output is correct
83 Correct 65 ms 17236 KB Output is correct
84 Correct 61 ms 17236 KB Output is correct
85 Correct 64 ms 17240 KB Output is correct
86 Correct 64 ms 17232 KB Output is correct
87 Correct 64 ms 17212 KB Output is correct
88 Correct 68 ms 16984 KB Output is correct
89 Correct 62 ms 17020 KB Output is correct
90 Correct 64 ms 16984 KB Output is correct
91 Correct 70 ms 17264 KB Output is correct
92 Correct 66 ms 16968 KB Output is correct