Submission #530368

# Submission time Handle Problem Language Result Execution time Memory
530368 2022-02-25T08:27:10 Z Sharky Mecho (IOI09_mecho) C++17
100 / 100
243 ms 6812 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' || a[x][y] == 'H'));
}
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;
            if (((now.t + 1) / s) + t >= dist[x][y] && (make_pair(x, y) != dest)) continue;
            // cout << now.t / s << " " << dist[x][y] << "\n";
            if (!vis[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 (!ok2(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 Correct 3 ms 5452 KB Output is correct
3 Correct 3 ms 5452 KB Output is correct
4 Correct 3 ms 5452 KB Output is correct
5 Correct 3 ms 5452 KB Output is correct
6 Correct 3 ms 5452 KB Output is correct
7 Correct 154 ms 6440 KB Output is correct
8 Correct 3 ms 5452 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 Correct 4 ms 5452 KB Output is correct
13 Correct 3 ms 5452 KB Output is correct
14 Correct 3 ms 5452 KB Output is correct
15 Correct 4 ms 5548 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 4 ms 5452 KB Output is correct
20 Correct 4 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 4 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 5452 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 9 ms 5684 KB Output is correct
34 Correct 8 ms 5580 KB Output is correct
35 Correct 33 ms 5692 KB Output is correct
36 Correct 7 ms 5708 KB Output is correct
37 Correct 8 ms 5708 KB Output is correct
38 Correct 41 ms 5728 KB Output is correct
39 Correct 8 ms 5768 KB Output is correct
40 Correct 10 ms 5708 KB Output is correct
41 Correct 68 ms 5764 KB Output is correct
42 Correct 9 ms 5708 KB Output is correct
43 Correct 11 ms 5816 KB Output is correct
44 Correct 77 ms 5812 KB Output is correct
45 Correct 10 ms 5836 KB Output is correct
46 Correct 13 ms 5868 KB Output is correct
47 Correct 79 ms 5876 KB Output is correct
48 Correct 12 ms 5936 KB Output is correct
49 Correct 16 ms 5836 KB Output is correct
50 Correct 97 ms 5948 KB Output is correct
51 Correct 14 ms 5964 KB Output is correct
52 Correct 23 ms 6056 KB Output is correct
53 Correct 132 ms 6020 KB Output is correct
54 Correct 18 ms 5964 KB Output is correct
55 Correct 23 ms 6068 KB Output is correct
56 Correct 140 ms 6084 KB Output is correct
57 Correct 24 ms 6092 KB Output is correct
58 Correct 32 ms 6144 KB Output is correct
59 Correct 185 ms 6164 KB Output is correct
60 Correct 18 ms 6220 KB Output is correct
61 Correct 23 ms 6224 KB Output is correct
62 Correct 177 ms 6240 KB Output is correct
63 Correct 127 ms 6228 KB Output is correct
64 Correct 243 ms 6236 KB Output is correct
65 Correct 199 ms 6228 KB Output is correct
66 Correct 141 ms 6228 KB Output is correct
67 Correct 37 ms 6220 KB Output is correct
68 Correct 57 ms 6276 KB Output is correct
69 Correct 52 ms 6292 KB Output is correct
70 Correct 46 ms 6220 KB Output is correct
71 Correct 34 ms 6220 KB Output is correct
72 Correct 40 ms 6288 KB Output is correct
73 Correct 49 ms 6812 KB Output is correct
74 Correct 132 ms 6732 KB Output is correct
75 Correct 96 ms 6732 KB Output is correct
76 Correct 84 ms 6732 KB Output is correct
77 Correct 94 ms 6812 KB Output is correct
78 Correct 36 ms 6656 KB Output is correct
79 Correct 110 ms 6736 KB Output is correct
80 Correct 102 ms 6740 KB Output is correct
81 Correct 93 ms 6764 KB Output is correct
82 Correct 85 ms 6756 KB Output is correct
83 Correct 150 ms 6616 KB Output is correct
84 Correct 113 ms 6652 KB Output is correct
85 Correct 98 ms 6656 KB Output is correct
86 Correct 106 ms 6660 KB Output is correct
87 Correct 156 ms 6660 KB Output is correct
88 Correct 114 ms 6552 KB Output is correct
89 Correct 125 ms 6552 KB Output is correct
90 Correct 139 ms 6548 KB Output is correct
91 Correct 140 ms 6552 KB Output is correct
92 Correct 118 ms 6548 KB Output is correct