Submission #839377

# Submission time Handle Problem Language Result Execution time Memory
839377 2023-08-29T22:41:26 Z ArashJafariyan Mecho (IOI09_mecho) C++17
54 / 100
185 ms 6476 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

#define F first
#define S second
#define pb push_back
#define size(a) (int) a.size()
#define all(a) a.begin(), a.end()

const int maxn = 800 + 4, inf = 1e9;

int n, s, mx, my, dx, dy, d[maxn][maxn], save[maxn][maxn];
bitset<maxn> vis[maxn];
string a[maxn];
queue<array<int, 2>> q;

void rest() {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            vis[i][j] = 0;
            d[i][j] = inf;
        }
    }
    return;
}

bool check0(int x, int y, int dis) {
    bool ret = (0 <= x && x < n);
    ret &= (0 <= y && y < n);
    if (ret) {
        ret &= !vis[x][y];
        if (ret && a[x][y] == 'D') {
            d[x][y] = dis;
            return 0;
        }
        ret &= (a[x][y] == 'G' || a[x][y] == 'M');
    }
    return ret;
}

void upd0(int x, int y) { 
    int dis = d[x][y] + 1;

    if (check0(x - 1, y, dis)) {
        vis[x - 1][y] = 1;
        d[x - 1][y] = dis;
        q.push({x - 1, y});
    }
    if (check0(x + 1, y, dis)) {
        vis[x + 1][y] = 1;
        d[x + 1][y] = dis;
        q.push({x + 1, y});
    }
    if (check0(x, y - 1, dis)) {
        vis[x][y - 1] = 1;
        d[x][y - 1] = dis;
        q.push({x, y - 1});
    }
    if (check0(x, y + 1, dis)) {
        vis[x][y + 1] = 1;
        d[x][y + 1] = dis;
        q.push({x, y + 1});
    }
}

bool check1(int x, int y, int dis, int k) {
    bool ret = (0 <= x && x < n);
    ret &= (0 <= y && y < n);
    if (ret) {
        ret &= !vis[x][y];
        ret &= a[x][y] == 'G' || a[x][y] == 'D';
        ret &= (dis + s - 1) / s <= save[x][y] - k;
    }
    return ret;
}

void upd1(int x, int y, int k) {
    int dis = d[x][y] + 1;
    
    if (check1(x - 1, y, dis, k)) {
        vis[x - 1][y] = 1;
        d[x - 1][y] = dis;
        q.push({x - 1, y});
    }
    if (check1(x + 1, y, dis, k)) {
        vis[x + 1][y] = 1;
        d[x + 1][y] = dis;
        q.push({x + 1, y});
    }
    if (check1(x, y - 1, dis, k)) {
        vis[x][y - 1] = 1;
        d[x][y - 1] = dis;
        q.push({x, y - 1});
    }
    if (check1(x, y + 1, dis, k)) {
        vis[x][y + 1] = 1;
        d[x][y + 1] = dis;
        q.push({x, y + 1});
    }
}

bool bfs(int k) {
    rest();

    if (save[mx][my] <= k) {
        return 0;
    }

    d[mx][my] = 0;
    vis[mx][my] = 1;
    q.push({mx, my});
    while (!q.empty()) {
        array<int, 2> v = q.front();
        q.pop();
        upd1(v[0], v[1], k);
    }
    
    return d[dx][dy] != inf;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);

    cin >> n >> s;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] == 'H') {
                q.push({i, j});
                vis[i][j] = 1;
            }
        }
    }

    while (!q.empty()) {
        array<int, 2> v = q.front();
        q.pop();
        upd0(v[0], v[1]);
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] == 'M') {
                mx = i;
                my = j;
            }
            if (a[i][j] == 'D') {
                dx = i;
                dy = j;
            }
            save[i][j] = d[i][j];
        }
    }

    int l, r;
    l = -1;
    r = inf;

    while (r - l > 1) {
        int m = (l + r) >> 1;
        if (bfs(m)) {
            l = m;
        }
        else {
            r = m;
        }
    }
    cout << l << '\n';

    return 0;
}

# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Incorrect 0 ms 340 KB Output isn't correct
4 Incorrect 0 ms 340 KB Output isn't correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 116 ms 6240 KB Output is correct
8 Incorrect 1 ms 340 KB Output isn't correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Incorrect 0 ms 468 KB Output isn't correct
16 Correct 1 ms 468 KB Output is correct
17 Incorrect 1 ms 468 KB Output isn't correct
18 Correct 1 ms 468 KB Output is correct
19 Incorrect 1 ms 468 KB Output isn't correct
20 Correct 0 ms 468 KB Output is correct
21 Incorrect 1 ms 596 KB Output isn't correct
22 Correct 1 ms 596 KB Output is correct
23 Incorrect 1 ms 596 KB Output isn't correct
24 Correct 1 ms 596 KB Output is correct
25 Incorrect 1 ms 724 KB Output isn't correct
26 Correct 1 ms 724 KB Output is correct
27 Incorrect 1 ms 724 KB Output isn't correct
28 Correct 1 ms 724 KB Output is correct
29 Incorrect 1 ms 724 KB Output isn't correct
30 Correct 1 ms 724 KB Output is correct
31 Incorrect 1 ms 724 KB Output isn't correct
32 Correct 1 ms 724 KB Output is correct
33 Incorrect 10 ms 2720 KB Output isn't correct
34 Correct 11 ms 2644 KB Output is correct
35 Correct 28 ms 2644 KB Output is correct
36 Incorrect 13 ms 3080 KB Output isn't correct
37 Correct 12 ms 3080 KB Output is correct
38 Correct 37 ms 3028 KB Output is correct
39 Incorrect 15 ms 3456 KB Output isn't correct
40 Correct 16 ms 3456 KB Output is correct
41 Correct 47 ms 3444 KB Output is correct
42 Incorrect 17 ms 3796 KB Output isn't correct
43 Correct 19 ms 3816 KB Output is correct
44 Correct 61 ms 3800 KB Output is correct
45 Incorrect 23 ms 4180 KB Output isn't correct
46 Correct 25 ms 4164 KB Output is correct
47 Correct 73 ms 4180 KB Output is correct
48 Incorrect 27 ms 4564 KB Output isn't correct
49 Correct 27 ms 4564 KB Output is correct
50 Correct 106 ms 4556 KB Output is correct
51 Incorrect 32 ms 4960 KB Output isn't correct
52 Correct 34 ms 4948 KB Output is correct
53 Correct 106 ms 4948 KB Output is correct
54 Incorrect 38 ms 5452 KB Output isn't correct
55 Correct 39 ms 5332 KB Output is correct
56 Correct 119 ms 5324 KB Output is correct
57 Incorrect 42 ms 5716 KB Output isn't correct
58 Correct 37 ms 5728 KB Output is correct
59 Correct 146 ms 5732 KB Output is correct
60 Incorrect 45 ms 6100 KB Output isn't correct
61 Correct 45 ms 6100 KB Output is correct
62 Correct 157 ms 6056 KB Output is correct
63 Correct 123 ms 6056 KB Output is correct
64 Correct 185 ms 6124 KB Output is correct
65 Correct 179 ms 6124 KB Output is correct
66 Incorrect 133 ms 6120 KB Output isn't correct
67 Correct 128 ms 6168 KB Output is correct
68 Correct 70 ms 6152 KB Output is correct
69 Correct 69 ms 6156 KB Output is correct
70 Correct 54 ms 6084 KB Output is correct
71 Correct 58 ms 6152 KB Output is correct
72 Correct 56 ms 6152 KB Output is correct
73 Correct 65 ms 6448 KB Output is correct
74 Correct 88 ms 6356 KB Output is correct
75 Correct 95 ms 6400 KB Output is correct
76 Correct 89 ms 6408 KB Output is correct
77 Correct 91 ms 6400 KB Output is correct
78 Correct 99 ms 6356 KB Output is correct
79 Correct 84 ms 6376 KB Output is correct
80 Correct 92 ms 6380 KB Output is correct
81 Correct 101 ms 6476 KB Output is correct
82 Correct 86 ms 6368 KB Output is correct
83 Correct 111 ms 6332 KB Output is correct
84 Correct 100 ms 6432 KB Output is correct
85 Correct 99 ms 6336 KB Output is correct
86 Correct 106 ms 6356 KB Output is correct
87 Correct 103 ms 6228 KB Output is correct
88 Correct 117 ms 6248 KB Output is correct
89 Correct 109 ms 6288 KB Output is correct
90 Correct 120 ms 6356 KB Output is correct
91 Correct 111 ms 6212 KB Output is correct
92 Correct 120 ms 6288 KB Output is correct