Submission #530367

# Submission time Handle Problem Language Result Execution time Memory
530367 2022-02-25T08:25:07 Z Sharky Mecho (IOI09_mecho) C++17
95 / 100
196 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'));
}
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 (!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 Correct 3 ms 5452 KB Output is correct
3 Correct 5 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 122 ms 6348 KB Output is correct
8 Correct 5 ms 5580 KB Output is correct
9 Correct 3 ms 5452 KB Output is correct
10 Correct 3 ms 5504 KB Output is correct
11 Correct 3 ms 5452 KB Output is correct
12 Incorrect 4 ms 5520 KB Output isn't correct
13 Correct 4 ms 5452 KB Output is correct
14 Correct 3 ms 5452 KB Output is correct
15 Correct 4 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 5540 KB Output is correct
20 Correct 3 ms 5452 KB Output is correct
21 Correct 4 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 5 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 3 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 7 ms 5580 KB Output is correct
34 Correct 8 ms 5688 KB Output is correct
35 Correct 32 ms 5688 KB Output is correct
36 Correct 11 ms 5724 KB Output is correct
37 Correct 8 ms 5708 KB Output is correct
38 Correct 41 ms 5724 KB Output is correct
39 Correct 8 ms 5708 KB Output is correct
40 Correct 9 ms 5708 KB Output is correct
41 Correct 51 ms 5708 KB Output is correct
42 Correct 9 ms 5708 KB Output is correct
43 Correct 10 ms 5708 KB Output is correct
44 Correct 66 ms 5808 KB Output is correct
45 Correct 10 ms 5860 KB Output is correct
46 Correct 15 ms 5868 KB Output is correct
47 Correct 78 ms 5868 KB Output is correct
48 Correct 12 ms 5836 KB Output is correct
49 Correct 16 ms 5836 KB Output is correct
50 Correct 112 ms 5936 KB Output is correct
51 Correct 18 ms 5920 KB Output is correct
52 Correct 19 ms 5964 KB Output is correct
53 Correct 115 ms 6012 KB Output is correct
54 Correct 17 ms 6068 KB Output is correct
55 Correct 22 ms 6072 KB Output is correct
56 Correct 146 ms 6084 KB Output is correct
57 Correct 16 ms 6092 KB Output is correct
58 Correct 21 ms 6092 KB Output is correct
59 Correct 153 ms 6164 KB Output is correct
60 Correct 18 ms 6220 KB Output is correct
61 Correct 25 ms 6228 KB Output is correct
62 Correct 196 ms 6224 KB Output is correct
63 Correct 135 ms 6116 KB Output is correct
64 Correct 187 ms 6224 KB Output is correct
65 Correct 192 ms 6360 KB Output is correct
66 Correct 133 ms 6220 KB Output is correct
67 Correct 36 ms 6220 KB Output is correct
68 Correct 54 ms 6272 KB Output is correct
69 Correct 49 ms 6248 KB Output is correct
70 Correct 42 ms 6220 KB Output is correct
71 Correct 34 ms 6272 KB Output is correct
72 Correct 36 ms 6264 KB Output is correct
73 Correct 44 ms 6760 KB Output is correct
74 Correct 95 ms 6756 KB Output is correct
75 Correct 90 ms 6812 KB Output is correct
76 Correct 81 ms 6732 KB Output is correct
77 Correct 96 ms 6808 KB Output is correct
78 Correct 34 ms 6732 KB Output is correct
79 Correct 87 ms 6732 KB Output is correct
80 Correct 81 ms 6744 KB Output is correct
81 Correct 89 ms 6740 KB Output is correct
82 Correct 74 ms 6732 KB Output is correct
83 Correct 116 ms 6768 KB Output is correct
84 Correct 117 ms 6656 KB Output is correct
85 Correct 99 ms 6604 KB Output is correct
86 Correct 110 ms 6604 KB Output is correct
87 Correct 126 ms 6660 KB Output is correct
88 Correct 118 ms 6596 KB Output is correct
89 Correct 124 ms 6476 KB Output is correct
90 Correct 118 ms 6548 KB Output is correct
91 Correct 120 ms 6476 KB Output is correct
92 Correct 116 ms 6552 KB Output is correct