Submission #886627

# Submission time Handle Problem Language Result Execution time Memory
886627 2023-12-12T13:00:43 Z gnu Mecho (IOI09_mecho) C++17
85 / 100
175 ms 11864 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][u.j] - 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 Incorrect 0 ms 348 KB Output isn't correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 97 ms 11356 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 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 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 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 0 ms 348 KB Output is correct
28 Correct 1 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 344 KB Output is correct
33 Correct 6 ms 2548 KB Output is correct
34 Correct 7 ms 2584 KB Output is correct
35 Correct 20 ms 2560 KB Output is correct
36 Correct 7 ms 3160 KB Output is correct
37 Correct 7 ms 3160 KB Output is correct
38 Correct 26 ms 3160 KB Output is correct
39 Correct 9 ms 3900 KB Output is correct
40 Correct 9 ms 3904 KB Output is correct
41 Correct 34 ms 3896 KB Output is correct
42 Correct 10 ms 4716 KB Output is correct
43 Correct 10 ms 4724 KB Output is correct
44 Correct 45 ms 4896 KB Output is correct
45 Correct 13 ms 5596 KB Output is correct
46 Correct 13 ms 5612 KB Output is correct
47 Correct 54 ms 5596 KB Output is correct
48 Correct 16 ms 6860 KB Output is correct
49 Correct 21 ms 6604 KB Output is correct
50 Correct 64 ms 6588 KB Output is correct
51 Correct 18 ms 7668 KB Output is correct
52 Correct 20 ms 7636 KB Output is correct
53 Correct 76 ms 7596 KB Output is correct
54 Correct 22 ms 8812 KB Output is correct
55 Correct 24 ms 8772 KB Output is correct
56 Correct 94 ms 8812 KB Output is correct
57 Correct 24 ms 10028 KB Output is correct
58 Correct 22 ms 10016 KB Output is correct
59 Correct 112 ms 10012 KB Output is correct
60 Correct 26 ms 11328 KB Output is correct
61 Correct 27 ms 11284 KB Output is correct
62 Correct 127 ms 11380 KB Output is correct
63 Incorrect 93 ms 11280 KB Output isn't correct
64 Incorrect 175 ms 11312 KB Output isn't correct
65 Correct 167 ms 11324 KB Output is correct
66 Correct 120 ms 11312 KB Output is correct
67 Correct 107 ms 11316 KB Output is correct
68 Correct 64 ms 11352 KB Output is correct
69 Incorrect 62 ms 11368 KB Output isn't correct
70 Correct 48 ms 11436 KB Output is correct
71 Correct 51 ms 11396 KB Output is correct
72 Correct 48 ms 11360 KB Output is correct
73 Correct 58 ms 11652 KB Output is correct
74 Correct 69 ms 11672 KB Output is correct
75 Correct 78 ms 11356 KB Output is correct
76 Correct 70 ms 11396 KB Output is correct
77 Correct 78 ms 11668 KB Output is correct
78 Correct 77 ms 11368 KB Output is correct
79 Correct 65 ms 11380 KB Output is correct
80 Correct 71 ms 11428 KB Output is correct
81 Correct 77 ms 11396 KB Output is correct
82 Correct 67 ms 11400 KB Output is correct
83 Correct 93 ms 11424 KB Output is correct
84 Correct 78 ms 11416 KB Output is correct
85 Correct 79 ms 11372 KB Output is correct
86 Correct 84 ms 11416 KB Output is correct
87 Correct 81 ms 11376 KB Output is correct
88 Correct 94 ms 11864 KB Output is correct
89 Correct 88 ms 11188 KB Output is correct
90 Correct 91 ms 11404 KB Output is correct
91 Correct 90 ms 11396 KB Output is correct
92 Correct 92 ms 11348 KB Output is correct