Submission #530366

# Submission time Handle Problem Language Result Execution time Memory
530366 2022-02-25T08:18:09 Z Sharky Mecho (IOI09_mecho) C++17
84 / 100
183 ms 6864 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
const int N = 805, dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
int n, s;
pair<int, int> src, dest;
vector<string> a(N); vector<vector<int> > dist(N, vector<int> (N, -1));
vector<pair<int, int> > hive; vector<vector<bool> > vis(N, vector<bool> (N, false));
bool ok(int x, int y) {
    return (x >= 0 && x < n && y >= 0 && y < n && (a[x][y] == 'G' || a[x][y] == 'M' || a[x][y] == 'D'));
}
bool ok2(int x, int y) {
    return (x >= 0 && x < n && y >= 0 && y < n && (a[x][y] == 'G' || a[x][y] == 'M'));
}
struct node {
    int x, y, t; 
    node(int a, int b, int c) {
        x = a, y = b, t = c;
    }
};
bool check(int t) {
    // cout << t << "\n";
    if (dist[src.fi][src.se] <= t) return false;
    queue<node> q; 
    for (int i = 0; i < n; i++) { fill(vis[i].begin(), vis[i].end(), false); }
    // for (auto& [fi, se] : hive) q.push(node(fi, se, 0)), vis[fi][se] = false;
    q.push(node(src.fi, src.se, 0)); vis[src.fi][src.se] = true;
    while (!q.empty()) {
        auto now = q.front();
        q.pop();
        // if (t == 24) cout << now.x << " " << now.y << " " << now.t << "\n";
        for (int i = 0; i < 4; i++) {
            int x = now.x + dx[i];
            int y = now.y + dy[i];
            // walk first then bees go
            // bees are at place (now.t - 1) / s;
            if (!ok(x, y)) continue;
            // cout << now.t / s << " " << dist[x][y] << "\n";
            if (!vis[x][y] && ((now.t + 1) / s) + t < dist[x][y]) {
                vis[x][y] = true;
                q.push(node(x, y, now.t + 1));
            }
        }
    }
    return vis[dest.fi][dest.se];
}
void precompute() {
    queue<pair<int, int> > q;
    for (int i = 0; i < n; i++) { fill(vis[i].begin(), vis[i].end(), false); }
    for (auto& [fi, se] : hive) q.push({fi, se}), vis[fi][se] = true, dist[fi][se] = 0;
    while (!q.empty()) {
        auto now = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            int x = now.fi + dx[i];
            int y = now.se + dy[i];
            if (!ok(x, y)) continue;
            if (!vis[x][y]) {
                vis[x][y] = true;
                q.push({x, y});
                dist[x][y] = dist[now.fi][now.se] + 1;
            }
        }
    }
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> s;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int j = 0; j < n; j++) {
            if (a[i][j] == 'M') src = {i, j};
            if (a[i][j] == 'H') hive.push_back({i, j});
            if (a[i][j] == 'D') dest = {i, j};
        }
    }
    precompute();
    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j < n; j++) {
    //         cout << vis[i][j] << " ";
    //     }
    //     cout << "\n";
    // }
    if (!check(0)) { cout << "-1\n"; return 0; }
    int l = 0, r = 1e9;
    while (l < r) {
        int m = (l + r) >> 1;
        if (!check(m)) r = m;
        else l = m + 1;
    }
    cout << l - 1 << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5452 KB Output is correct
2 Incorrect 3 ms 5452 KB Output isn't correct
3 Correct 3 ms 5452 KB Output is correct
4 Incorrect 3 ms 5452 KB Output isn't correct
5 Correct 3 ms 5452 KB Output is correct
6 Incorrect 3 ms 5452 KB Output isn't correct
7 Incorrect 110 ms 6468 KB Output isn't correct
8 Correct 3 ms 5472 KB Output is correct
9 Correct 3 ms 5452 KB Output is correct
10 Correct 4 ms 5452 KB Output is correct
11 Correct 3 ms 5452 KB Output is correct
12 Incorrect 3 ms 5464 KB Output isn't correct
13 Correct 3 ms 5452 KB Output is correct
14 Correct 3 ms 5452 KB Output is correct
15 Correct 3 ms 5452 KB Output is correct
16 Correct 3 ms 5452 KB Output is correct
17 Correct 3 ms 5452 KB Output is correct
18 Correct 3 ms 5452 KB Output is correct
19 Correct 3 ms 5452 KB Output is correct
20 Correct 3 ms 5452 KB Output is correct
21 Correct 3 ms 5452 KB Output is correct
22 Correct 3 ms 5452 KB Output is correct
23 Correct 3 ms 5452 KB Output is correct
24 Correct 3 ms 5452 KB Output is correct
25 Correct 3 ms 5452 KB Output is correct
26 Correct 3 ms 5452 KB Output is correct
27 Correct 3 ms 5484 KB Output is correct
28 Correct 3 ms 5452 KB Output is correct
29 Correct 3 ms 5452 KB Output is correct
30 Correct 4 ms 5452 KB Output is correct
31 Correct 3 ms 5452 KB Output is correct
32 Correct 3 ms 5452 KB Output is correct
33 Correct 6 ms 5580 KB Output is correct
34 Correct 8 ms 5580 KB Output is correct
35 Correct 29 ms 5696 KB Output is correct
36 Correct 10 ms 5724 KB Output is correct
37 Correct 8 ms 5724 KB Output is correct
38 Correct 38 ms 5708 KB Output is correct
39 Correct 8 ms 5708 KB Output is correct
40 Correct 10 ms 5768 KB Output is correct
41 Correct 45 ms 5708 KB Output is correct
42 Correct 8 ms 5708 KB Output is correct
43 Correct 10 ms 5708 KB Output is correct
44 Correct 63 ms 5828 KB Output is correct
45 Correct 10 ms 5836 KB Output is correct
46 Correct 12 ms 5864 KB Output is correct
47 Correct 72 ms 5836 KB Output is correct
48 Correct 12 ms 5940 KB Output is correct
49 Correct 15 ms 5944 KB Output is correct
50 Correct 85 ms 5948 KB Output is correct
51 Correct 13 ms 5964 KB Output is correct
52 Correct 19 ms 6004 KB Output is correct
53 Correct 104 ms 6084 KB Output is correct
54 Correct 17 ms 5964 KB Output is correct
55 Correct 22 ms 6064 KB Output is correct
56 Correct 115 ms 5964 KB Output is correct
57 Correct 16 ms 6152 KB Output is correct
58 Correct 19 ms 6144 KB Output is correct
59 Correct 149 ms 6164 KB Output is correct
60 Correct 18 ms 6232 KB Output is correct
61 Correct 23 ms 6220 KB Output is correct
62 Correct 159 ms 6360 KB Output is correct
63 Correct 123 ms 6224 KB Output is correct
64 Correct 183 ms 6220 KB Output is correct
65 Correct 181 ms 6232 KB Output is correct
66 Correct 137 ms 6220 KB Output is correct
67 Correct 35 ms 6148 KB Output is correct
68 Correct 52 ms 6284 KB Output is correct
69 Correct 48 ms 6220 KB Output is correct
70 Correct 41 ms 6220 KB Output is correct
71 Correct 34 ms 6220 KB Output is correct
72 Correct 36 ms 6220 KB Output is correct
73 Correct 44 ms 6812 KB Output is correct
74 Correct 88 ms 6752 KB Output is correct
75 Correct 83 ms 6820 KB Output is correct
76 Correct 72 ms 6808 KB Output is correct
77 Correct 79 ms 6820 KB Output is correct
78 Correct 35 ms 6628 KB Output is correct
79 Correct 101 ms 6864 KB Output is correct
80 Correct 71 ms 6732 KB Output is correct
81 Correct 80 ms 6748 KB Output is correct
82 Correct 69 ms 6736 KB Output is correct
83 Correct 98 ms 6604 KB Output is correct
84 Correct 101 ms 6724 KB Output is correct
85 Correct 84 ms 6724 KB Output is correct
86 Correct 92 ms 6660 KB Output is correct
87 Correct 91 ms 6664 KB Output is correct
88 Correct 97 ms 6504 KB Output is correct
89 Correct 108 ms 6596 KB Output is correct
90 Correct 108 ms 6552 KB Output is correct
91 Correct 103 ms 6552 KB Output is correct
92 Correct 101 ms 6476 KB Output is correct