Submission #530362

# Submission time Handle Problem Language Result Execution time Memory
530362 2022-02-25T08:03:53 Z Sharky Mecho (IOI09_mecho) C++17
73 / 100
220 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'));
}
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";
    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 108 ms 6444 KB Output isn't correct
8 Correct 4 ms 5452 KB Output is correct
9 Correct 3 ms 5452 KB Output is correct
10 Correct 3 ms 5516 KB Output is correct
11 Correct 3 ms 5452 KB Output is correct
12 Incorrect 4 ms 5604 KB Output isn't correct
13 Incorrect 3 ms 5452 KB Output isn't 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 5556 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 5556 KB Output is correct
26 Correct 4 ms 5528 KB Output is correct
27 Correct 4 ms 5452 KB Output is correct
28 Correct 3 ms 5452 KB Output is correct
29 Correct 5 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 7 ms 5688 KB Output is correct
34 Correct 8 ms 5580 KB Output is correct
35 Correct 28 ms 5580 KB Output is correct
36 Correct 7 ms 5708 KB Output is correct
37 Correct 9 ms 5708 KB Output is correct
38 Correct 36 ms 5708 KB Output is correct
39 Correct 9 ms 5836 KB Output is correct
40 Correct 10 ms 5708 KB Output is correct
41 Correct 59 ms 5768 KB Output is correct
42 Correct 8 ms 5708 KB Output is correct
43 Correct 11 ms 5824 KB Output is correct
44 Correct 61 ms 5820 KB Output is correct
45 Correct 11 ms 5836 KB Output is correct
46 Correct 13 ms 5868 KB Output is correct
47 Correct 72 ms 5876 KB Output is correct
48 Correct 12 ms 5940 KB Output is correct
49 Correct 16 ms 5836 KB Output is correct
50 Correct 91 ms 5944 KB Output is correct
51 Correct 14 ms 5964 KB Output is correct
52 Correct 17 ms 6008 KB Output is correct
53 Correct 109 ms 6004 KB Output is correct
54 Correct 24 ms 6072 KB Output is correct
55 Correct 23 ms 5964 KB Output is correct
56 Correct 116 ms 6072 KB Output is correct
57 Correct 16 ms 6092 KB Output is correct
58 Correct 18 ms 6148 KB Output is correct
59 Correct 132 ms 6092 KB Output is correct
60 Correct 18 ms 6220 KB Output is correct
61 Correct 26 ms 6232 KB Output is correct
62 Correct 220 ms 6232 KB Output is correct
63 Correct 123 ms 6224 KB Output is correct
64 Correct 185 ms 6220 KB Output is correct
65 Correct 215 ms 6232 KB Output is correct
66 Correct 131 ms 6220 KB Output is correct
67 Correct 35 ms 6236 KB Output is correct
68 Correct 52 ms 6220 KB Output is correct
69 Correct 48 ms 6220 KB Output is correct
70 Correct 47 ms 6280 KB Output is correct
71 Correct 41 ms 6220 KB Output is correct
72 Incorrect 35 ms 6220 KB Output isn't correct
73 Incorrect 49 ms 6812 KB Output isn't correct
74 Correct 83 ms 6732 KB Output is correct
75 Correct 95 ms 6732 KB Output is correct
76 Correct 77 ms 6744 KB Output is correct
77 Correct 78 ms 6732 KB Output is correct
78 Correct 34 ms 6740 KB Output is correct
79 Correct 77 ms 6744 KB Output is correct
80 Correct 77 ms 6740 KB Output is correct
81 Correct 83 ms 6744 KB Output is correct
82 Correct 99 ms 6752 KB Output is correct
83 Correct 93 ms 6612 KB Output is correct
84 Correct 93 ms 6604 KB Output is correct
85 Correct 84 ms 6604 KB Output is correct
86 Correct 107 ms 6656 KB Output is correct
87 Correct 122 ms 6652 KB Output is correct
88 Correct 111 ms 6476 KB Output is correct
89 Correct 106 ms 6492 KB Output is correct
90 Correct 105 ms 6544 KB Output is correct
91 Correct 152 ms 6564 KB Output is correct
92 Correct 111 ms 6656 KB Output is correct