Submission #783885

# Submission time Handle Problem Language Result Execution time Memory
783885 2023-07-15T11:47:34 Z VMaksimoski008 Mecho (IOI09_mecho) C++17
100 / 100
160 ms 6880 KB
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <cstring>

#define pb push_back
#define sz(x) (int)x.size()
#define mp make_pair

using namespace std;

using ll = long long;
using pii = pair<int, int>;

void setIO() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

int n, s;
int dr[4] = {0, -1, 0, 1};
int dc[4] = {-1, 0, 1, 0};
char mat[800][800];
vector<pii> hives;
pii mecho, home;

int dist[800][800][2];
bool vis[800][800];

bool ok(int r, int c) {
    return (r >= 0 && r < n && c >= 0 && c < n && !vis[r][c] && mat[r][c] != 'T');
}

void hive_fill() {
    queue<pair<int, int> > q;
    for(pii &x : hives) {
        q.push(x);
        vis[x.first][x.second] = true;
    }

    while(!q.empty()) {
        int r = q.front().first;
        int c = q.front().second;
        q.pop();

        for(int i=0; i<4; i++) {
            int newR = r + dr[i];
            int newC = c + dc[i];
            if(!ok(newR, newC) || mat[newR][newC] == 'D') continue;
            vis[newR][newC] = true;
            dist[newR][newC][1] = dist[r][c][1] + 1;
            q.push({ newR, newC });
        }
    }
}

int calcMinutes(int steps) {
    return steps / s;
}

bool check(int mid) {
    queue<pii> q;
    memset(vis, 0, sizeof(vis));

    q.push(mecho);
    vis[mecho.first][mecho.second] = true;
    while(!q.empty()) {
        int r = q.front().first;
        int c = q.front().second;
        q.pop();

        if(r == home.first && c == home.second)
            return true;

        for(int i=0; i<4; i++) {
            int newR = r + dr[i];
            int newC = c + dc[i];
            if(!ok(newR, newC)) continue;
            if(mid >= dist[newR][newC][1] && mat[newR][newC] != 'D') 
                continue;
            if(mid + calcMinutes(dist[r][c][0] + 1) >= dist[newR][newC][1] && mat[newR][newC] != 'D')
                continue;
            q.push({ newR, newC });
            vis[newR][newC] = true;
            dist[newR][newC][0] = dist[r][c][0] + 1;
        }
    }

    return false;
}

int32_t main() {
    setIO();

    cin >> n >> s;
    memset(dist, 0, sizeof(dist));
    memset(vis, 0, sizeof(vis));
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            cin >> mat[i][j];
            if(mat[i][j] == 'H') hives.pb({i, j});
            else if(mat[i][j] == 'M') mecho = {i, j};
            else if(mat[i][j] == 'D') home = {i, j};
        }
    }

    hive_fill();

    int ans = -1;
    int l=0, r=dist[mecho.first][mecho.second][1]-1;
    while(l <= r) {
        int mid = (l + r) / 2;

        if(check(mid)) {
            ans = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }

    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5844 KB Output is correct
2 Correct 2 ms 5844 KB Output is correct
3 Correct 2 ms 5844 KB Output is correct
4 Correct 3 ms 5888 KB Output is correct
5 Correct 2 ms 5844 KB Output is correct
6 Correct 2 ms 5844 KB Output is correct
7 Correct 78 ms 6600 KB Output is correct
8 Correct 3 ms 5972 KB Output is correct
9 Correct 3 ms 5972 KB Output is correct
10 Correct 3 ms 5844 KB Output is correct
11 Correct 4 ms 5972 KB Output is correct
12 Correct 2 ms 5972 KB Output is correct
13 Correct 2 ms 5972 KB Output is correct
14 Correct 2 ms 5972 KB Output is correct
15 Correct 2 ms 5972 KB Output is correct
16 Correct 2 ms 5972 KB Output is correct
17 Correct 2 ms 5972 KB Output is correct
18 Correct 2 ms 5972 KB Output is correct
19 Correct 2 ms 5972 KB Output is correct
20 Correct 2 ms 5972 KB Output is correct
21 Correct 2 ms 5972 KB Output is correct
22 Correct 3 ms 5972 KB Output is correct
23 Correct 3 ms 5972 KB Output is correct
24 Correct 2 ms 5972 KB Output is correct
25 Correct 2 ms 5972 KB Output is correct
26 Correct 3 ms 5972 KB Output is correct
27 Correct 2 ms 5972 KB Output is correct
28 Correct 2 ms 5972 KB Output is correct
29 Correct 2 ms 5972 KB Output is correct
30 Correct 2 ms 5972 KB Output is correct
31 Correct 3 ms 5972 KB Output is correct
32 Correct 2 ms 5936 KB Output is correct
33 Correct 5 ms 6228 KB Output is correct
34 Correct 5 ms 6228 KB Output is correct
35 Correct 24 ms 6236 KB Output is correct
36 Correct 5 ms 6228 KB Output is correct
37 Correct 6 ms 6228 KB Output is correct
38 Correct 26 ms 6276 KB Output is correct
39 Correct 6 ms 6228 KB Output is correct
40 Correct 8 ms 6312 KB Output is correct
41 Correct 33 ms 6256 KB Output is correct
42 Correct 8 ms 6292 KB Output is correct
43 Correct 8 ms 6356 KB Output is correct
44 Correct 41 ms 6352 KB Output is correct
45 Correct 8 ms 6356 KB Output is correct
46 Correct 8 ms 6356 KB Output is correct
47 Correct 51 ms 6356 KB Output is correct
48 Correct 9 ms 6356 KB Output is correct
49 Correct 10 ms 6436 KB Output is correct
50 Correct 58 ms 6432 KB Output is correct
51 Correct 10 ms 6356 KB Output is correct
52 Correct 11 ms 6356 KB Output is correct
53 Correct 79 ms 6464 KB Output is correct
54 Correct 12 ms 6472 KB Output is correct
55 Correct 13 ms 6512 KB Output is correct
56 Correct 81 ms 6504 KB Output is correct
57 Correct 13 ms 6552 KB Output is correct
58 Correct 13 ms 6484 KB Output is correct
59 Correct 91 ms 6484 KB Output is correct
60 Correct 14 ms 6484 KB Output is correct
61 Correct 15 ms 6588 KB Output is correct
62 Correct 118 ms 6584 KB Output is correct
63 Correct 93 ms 6572 KB Output is correct
64 Correct 160 ms 6576 KB Output is correct
65 Correct 150 ms 6580 KB Output is correct
66 Correct 112 ms 6568 KB Output is correct
67 Correct 101 ms 6572 KB Output is correct
68 Correct 44 ms 6596 KB Output is correct
69 Correct 38 ms 6512 KB Output is correct
70 Correct 34 ms 6620 KB Output is correct
71 Correct 28 ms 6612 KB Output is correct
72 Correct 29 ms 6588 KB Output is correct
73 Correct 40 ms 6872 KB Output is correct
74 Correct 57 ms 6868 KB Output is correct
75 Correct 59 ms 6880 KB Output is correct
76 Correct 66 ms 6876 KB Output is correct
77 Correct 63 ms 6868 KB Output is correct
78 Correct 66 ms 6832 KB Output is correct
79 Correct 51 ms 6840 KB Output is correct
80 Correct 58 ms 6852 KB Output is correct
81 Correct 66 ms 6852 KB Output is correct
82 Correct 54 ms 6848 KB Output is correct
83 Correct 72 ms 6812 KB Output is correct
84 Correct 62 ms 6748 KB Output is correct
85 Correct 63 ms 6728 KB Output is correct
86 Correct 95 ms 6792 KB Output is correct
87 Correct 69 ms 6724 KB Output is correct
88 Correct 76 ms 6740 KB Output is correct
89 Correct 73 ms 6756 KB Output is correct
90 Correct 77 ms 6740 KB Output is correct
91 Correct 82 ms 6760 KB Output is correct
92 Correct 84 ms 6736 KB Output is correct