Submission #597579

# Submission time Handle Problem Language Result Execution time Memory
597579 2022-07-16T10:47:13 Z BhavayGoyal Mecho (IOI09_mecho) C++14
50 / 100
184 ms 8100 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;

// -------------------------------------------------- Main Code --------------------------------------------------

const int N = 800;
int n, s;
int arr[N][N], beesDis[N][N], bearDis[N][N];
queue<pii> bees;
pii st, en;

int di[4] = {0, 0, 1, -1}, dj[4] = {1, -1, 0, 0};

bool isVal(int i, int j) {
    return (i >= 0 && j >= 0 && i < n && j < n && arr[i][j]);
}

void fillBees() {
    while (!bees.empty()) {
        auto f = bees.front(); bees.pop();
        for (int i = 0; i < 4; i++) {
            int ni = f.f+di[i], nj = f.s+dj[i];
            if (isVal(ni, nj) && beesDis[ni][nj] == -1) {
                beesDis[ni][nj] = beesDis[f.f][f.s]+1;
                bees.push({ni, nj});
            }
        }
    }
}

bool isPoss(int i, int j, int dis, int initTime) {
    return ((dis/s)+initTime < beesDis[i][j]);
}

bool isFiss(int initTime) {
    if (beesDis[st.f][st.s] <= initTime) return false;
    memset(bearDis, -1, sizeof bearDis);
    queue<pii> bear; bear.push({st.f, st.s});
    bearDis[st.f][st.s] = 0;
    while (!bear.empty()) {
        auto f = bear.front(); bear.pop();
        for (int i = 0; i < 4; i++) {
            int ni = f.f+di[i], nj = f.s+dj[i];
            if (isVal(ni, nj) && bearDis[ni][nj] == -1 && isPoss(ni, nj, bearDis[f.f][f.s]+1, initTime)) {
                bearDis[ni][nj] = bearDis[f.f][f.s]+1;
                bear.push({ni, nj});
            }
        }
    }

    for (int i = 0; i < 4; i++) {
        int ni = en.f+di[i], nj = en.s+dj[i];
        if (isVal(ni, nj) && bearDis[ni][nj] != -1) return true;
    }

    return false;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> s;
    memset(beesDis, -1, sizeof beesDis);
    memset(arr, 0, sizeof arr);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            char x; cin >> x;
            if (x == 'M') st = {i, j};
            if (x == 'D') en = {i, j};
            if (x == 'H') {
                bees.push({i, j});
                beesDis[i][j] = 0;
            }
            if (x == 'G' || x == 'M') arr[i][j] = 1;
        }
    }

    fillBees();

    int i = 0, j = n, ans = inf;
    while (i <= j) {
        int mid = (i+j)/2;
        if (isFiss(mid)) ans = mid, i = mid+1;
        else j = mid-1;
    }
    cout << (ans==inf?-1:ans) << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7764 KB Output is correct
2 Correct 3 ms 7764 KB Output is correct
3 Correct 4 ms 7764 KB Output is correct
4 Correct 4 ms 7764 KB Output is correct
5 Correct 4 ms 7812 KB Output is correct
6 Correct 4 ms 7712 KB Output is correct
7 Correct 93 ms 7936 KB Output is correct
8 Correct 3 ms 7764 KB Output is correct
9 Correct 3 ms 7764 KB Output is correct
10 Correct 4 ms 7828 KB Output is correct
11 Correct 3 ms 7816 KB Output is correct
12 Incorrect 4 ms 7816 KB Output isn't correct
13 Correct 4 ms 7764 KB Output is correct
14 Correct 4 ms 7764 KB Output is correct
15 Incorrect 4 ms 7764 KB Output isn't correct
16 Incorrect 4 ms 7764 KB Output isn't correct
17 Incorrect 4 ms 7764 KB Output isn't correct
18 Incorrect 3 ms 7764 KB Output isn't correct
19 Incorrect 3 ms 7764 KB Output isn't correct
20 Incorrect 4 ms 7828 KB Output isn't correct
21 Incorrect 4 ms 7764 KB Output isn't correct
22 Incorrect 4 ms 7764 KB Output isn't correct
23 Incorrect 4 ms 7716 KB Output isn't correct
24 Incorrect 4 ms 7764 KB Output isn't correct
25 Incorrect 4 ms 7764 KB Output isn't correct
26 Incorrect 3 ms 7764 KB Output isn't correct
27 Incorrect 4 ms 7764 KB Output isn't correct
28 Incorrect 4 ms 7836 KB Output isn't correct
29 Incorrect 4 ms 7764 KB Output isn't correct
30 Incorrect 5 ms 7808 KB Output isn't correct
31 Incorrect 4 ms 7764 KB Output isn't correct
32 Incorrect 4 ms 7764 KB Output isn't correct
33 Incorrect 14 ms 7784 KB Output isn't correct
34 Incorrect 17 ms 7836 KB Output isn't correct
35 Correct 21 ms 7764 KB Output is correct
36 Incorrect 16 ms 7764 KB Output isn't correct
37 Incorrect 20 ms 7764 KB Output isn't correct
38 Correct 29 ms 7816 KB Output is correct
39 Incorrect 20 ms 7764 KB Output isn't correct
40 Incorrect 33 ms 7816 KB Output isn't correct
41 Correct 36 ms 7764 KB Output is correct
42 Incorrect 28 ms 7820 KB Output isn't correct
43 Incorrect 31 ms 7764 KB Output isn't correct
44 Correct 41 ms 7764 KB Output is correct
45 Incorrect 30 ms 7764 KB Output isn't correct
46 Incorrect 41 ms 7764 KB Output isn't correct
47 Correct 49 ms 7828 KB Output is correct
48 Incorrect 33 ms 7764 KB Output isn't correct
49 Incorrect 64 ms 7816 KB Output isn't correct
50 Correct 66 ms 7820 KB Output is correct
51 Incorrect 40 ms 7772 KB Output isn't correct
52 Incorrect 54 ms 7816 KB Output isn't correct
53 Correct 74 ms 7824 KB Output is correct
54 Incorrect 47 ms 7820 KB Output isn't correct
55 Incorrect 63 ms 7764 KB Output isn't correct
56 Correct 87 ms 7820 KB Output is correct
57 Incorrect 56 ms 7812 KB Output isn't correct
58 Incorrect 71 ms 7812 KB Output isn't correct
59 Correct 97 ms 7764 KB Output is correct
60 Incorrect 56 ms 7824 KB Output isn't correct
61 Incorrect 91 ms 7824 KB Output isn't correct
62 Correct 145 ms 7884 KB Output is correct
63 Correct 105 ms 7816 KB Output is correct
64 Incorrect 184 ms 7820 KB Output isn't correct
65 Correct 147 ms 7764 KB Output is correct
66 Correct 121 ms 7768 KB Output is correct
67 Correct 132 ms 7824 KB Output is correct
68 Correct 54 ms 7792 KB Output is correct
69 Correct 48 ms 7756 KB Output is correct
70 Correct 47 ms 7776 KB Output is correct
71 Correct 42 ms 7844 KB Output is correct
72 Correct 33 ms 7796 KB Output is correct
73 Correct 41 ms 8096 KB Output is correct
74 Correct 60 ms 8040 KB Output is correct
75 Correct 62 ms 8020 KB Output is correct
76 Correct 67 ms 8100 KB Output is correct
77 Correct 58 ms 8096 KB Output is correct
78 Correct 67 ms 8072 KB Output is correct
79 Correct 58 ms 8072 KB Output is correct
80 Correct 54 ms 8072 KB Output is correct
81 Correct 74 ms 8072 KB Output is correct
82 Correct 79 ms 8068 KB Output is correct
83 Correct 72 ms 8028 KB Output is correct
84 Correct 69 ms 7976 KB Output is correct
85 Correct 71 ms 8048 KB Output is correct
86 Correct 80 ms 8028 KB Output is correct
87 Correct 111 ms 8024 KB Output is correct
88 Correct 75 ms 7928 KB Output is correct
89 Correct 75 ms 7980 KB Output is correct
90 Correct 76 ms 7932 KB Output is correct
91 Correct 92 ms 7980 KB Output is correct
92 Correct 71 ms 7976 KB Output is correct