Submission #886629

# Submission time Handle Problem Language Result Execution time Memory
886629 2023-12-12T13:04:15 Z gnu Mecho (IOI09_mecho) C++17
100 / 100
175 ms 11728 KB
#include <bits/stdc++.h>
using namespace std;
int n, S;
vector<string> board;
vector<vector<int>> vis;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
struct point{
    int i, j;
    point(int i, int j) {
        this->i = i;
        this->j = j;
    }
};
void solve()
{
    cin >> n >> S;
    board = vector<string>(n);
    vis = vector<vector<int>> (n, vector<int> (n));
    for (int i = 0; i < n; ++i) cin >> board[i];
    point mecho(0,0), home(0,0);
    vector<point> honey;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (board[i][j] == 'H') honey.emplace_back(i, j);
            else if (board[i][j] == 'M') mecho = {i, j};
            else if (board[i][j] == 'D') home = {i, j};
        }
    }
    queue<point> mq;
    vector<vector<int>> dist(n, vector<int> (n, 0   ));
    for (auto x : honey) {
        mq.push(x);
        vis[x.i][x.j] = 1;
    }
    while (!mq.empty()) {
        point u = mq.front();
        mq.pop();
        for (int i = 0; i < 4; ++i) {
            if (u.i + dx[i] < 0 || u.i + dx[i] >= n || u.j + dy[i] < 0 || u.j + dy[i] >= n || vis[u.i+dx[i]][u.j+dy[i]] || (board[u.i+dx[i]][u.j+dy[i]] != 'G' && board[u.i+dx[i]][u.j+dy[i]] != 'M')) continue;
            mq.emplace(u.i + dx[i], u.j + dy[i]);
            vis[u.i + dx[i]][u.j + dy[i]] = 1;
            dist[u.i + dx[i]][u.j + dy[i]] = dist[u.i][u.j] + 1;
        }
    }
    //    for (auto &x : dist)
    //    {
    //        for (auto y : x) cout << y << ' ';
    //        cout << '\n';
    //    }

    int l = 0, r = 1e9;
    //    return;
    while (l <= r) {
        vector<vector<int>> md(n, vector<int> (n));
        int m = (l + r) / 2;
        vis = vector<vector<int>> (n, vector<int> (n));
        queue<point> q;
        q.emplace(mecho.i, mecho.j);
        vis[mecho.i][mecho.j] = 1;
        if (m >= dist[mecho.i][mecho.j]) q.pop();
        while (!q.empty()) {
            point u = q.front();

            q.pop();
            for (int i = 0; i < 4; ++i) {
                int ni = u.i + dx[i], nj = u.j + dy[i];
                if (u.i + dx[i] < 0 || u.i + dx[i] >= n || u.j + dy[i] < 0 || u.j + dy[i] >= n || vis[u.i+dx[i]][u.j+dy[i]] || (board[u.i+dx[i]][u.j+dy[i]] != 'G' && board[u.i+dx[i]][u.j+dy[i]] != 'M')) continue;
                if (((md[u.i][u.j]+1) / S >= (dist[u.i+dx[i]][u.j+dy[i]] - m))) continue;
                q.emplace(ni, nj);
                md[ni][nj] = md[u.i][u.j] + 1;
                vis[ni][nj] = 1;
            }
        }
        bool ok = false;
        for (int i = 0; i < 4; ++i) {
            point u = home;
            if (u.i + dx[i] < 0 || u.i + dx[i] >= n || u.j + dy[i] < 0 || u.j + dy[i] >= n || (board[u.i+dx[i]][u.j+dy[i]] != 'G' && board[u.i+dx[i]][u.j+dy[i]] != 'M')) continue;
            if (((md[u.i+dx[i]][u.j+dy[i]]) / S >= (dist[u.i + dx[i]][u.j + dy[i]] - m))) continue;
            if (vis[home.i + dx[i]][home.j+dy[i]]) ok = true;
        }
        if (ok) {
            l = m+1;
        } else r = m-1;

    }
    cout << l-1;

}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 94 ms 11440 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 428 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 424 KB Output is correct
33 Correct 7 ms 2548 KB Output is correct
34 Correct 7 ms 2548 KB Output is correct
35 Correct 20 ms 2556 KB Output is correct
36 Correct 7 ms 3164 KB Output is correct
37 Correct 7 ms 3164 KB Output is correct
38 Correct 28 ms 3204 KB Output is correct
39 Correct 12 ms 3936 KB Output is correct
40 Correct 9 ms 3880 KB Output is correct
41 Correct 33 ms 3956 KB Output is correct
42 Correct 10 ms 4720 KB Output is correct
43 Correct 16 ms 4688 KB Output is correct
44 Correct 47 ms 4732 KB Output is correct
45 Correct 16 ms 5580 KB Output is correct
46 Correct 16 ms 5608 KB Output is correct
47 Correct 54 ms 5628 KB Output is correct
48 Correct 15 ms 6584 KB Output is correct
49 Correct 16 ms 6608 KB Output is correct
50 Correct 64 ms 6576 KB Output is correct
51 Correct 18 ms 7668 KB Output is correct
52 Correct 20 ms 7668 KB Output is correct
53 Correct 75 ms 7600 KB Output is correct
54 Correct 22 ms 8812 KB Output is correct
55 Correct 24 ms 8768 KB Output is correct
56 Correct 98 ms 8912 KB Output is correct
57 Correct 24 ms 10016 KB Output is correct
58 Correct 24 ms 10016 KB Output is correct
59 Correct 113 ms 9992 KB Output is correct
60 Correct 26 ms 11336 KB Output is correct
61 Correct 28 ms 11324 KB Output is correct
62 Correct 120 ms 11324 KB Output is correct
63 Correct 109 ms 11324 KB Output is correct
64 Correct 175 ms 11308 KB Output is correct
65 Correct 175 ms 11312 KB Output is correct
66 Correct 132 ms 11632 KB Output is correct
67 Correct 112 ms 11312 KB Output is correct
68 Correct 66 ms 11360 KB Output is correct
69 Correct 62 ms 11376 KB Output is correct
70 Correct 48 ms 11348 KB Output is correct
71 Correct 52 ms 11356 KB Output is correct
72 Correct 47 ms 11404 KB Output is correct
73 Correct 55 ms 11384 KB Output is correct
74 Correct 79 ms 11352 KB Output is correct
75 Correct 79 ms 11400 KB Output is correct
76 Correct 73 ms 11384 KB Output is correct
77 Correct 72 ms 11408 KB Output is correct
78 Correct 95 ms 11372 KB Output is correct
79 Correct 76 ms 11376 KB Output is correct
80 Correct 69 ms 11292 KB Output is correct
81 Correct 97 ms 11728 KB Output is correct
82 Correct 66 ms 11384 KB Output is correct
83 Correct 87 ms 11416 KB Output is correct
84 Correct 77 ms 11376 KB Output is correct
85 Correct 91 ms 11364 KB Output is correct
86 Correct 88 ms 11420 KB Output is correct
87 Correct 85 ms 11364 KB Output is correct
88 Correct 86 ms 11388 KB Output is correct
89 Correct 91 ms 11424 KB Output is correct
90 Correct 99 ms 11384 KB Output is correct
91 Correct 91 ms 11384 KB Output is correct
92 Correct 94 ms 11396 KB Output is correct