Submission #145922

#TimeUsernameProblemLanguageResultExecution timeMemory
145922popovicirobertMecho (IOI09_mecho)C++14
100 / 100
291 ms7000 KiB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long


#if 0
const int MOD = ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;

const int INF = 1e9;
const int MAXN = 800;

char mat[MAXN + 1][MAXN + 1];
int dl[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1}, n;

bool in(int l, int c) {
    return l > 0 && c > 0 && l <= n && c <= n;
}

int main() {
#if 0
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, j, s;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> s;
    int lm, cm, ld, cd;
    for(i = 1; i <= n; i++) {
        cin >> mat[i] + 1;
        for(j = 1; j <= n; j++) {
            if(mat[i][j] == 'M') {
                lm = i, cm = j;
            }
            if(mat[i][j] == 'D') {
                ld = i, cd = j;
            }
        }
    }

    auto bfs = [&](vector < vector <int> > &dst, char ch) -> void {
        queue < pair <int, int> > Q;
        int i, j;
        for(i = 1; i <= n; i++) {
            for(j = 1; j <= n; j++) {
                dst[i][j] = INF;
                if(mat[i][j] == ch) {
                    Q.push({i, j});
                    dst[i][j] = 0;
                }
            }
        }
        while(Q.size()) {
            auto cur = Q.front();
            Q.pop();
            for(int i = 0; i < 4; i++) {
                int l = cur.first + dl[i], c = cur.second + dc[i];
                if(in(l, c) && dst[l][c] == INF && (mat[l][c] == 'G' || mat[l][c] == 'M')) {
                    dst[l][c] = dst[cur.first][cur.second] + 1;
                    Q.push({l, c});
                }
            }
        }
    };

    vector < vector <int> > H(n + 1, vector <int>(n + 1));
    vector < vector <int> > M(n + 1, vector <int>(n + 1));
    bfs(H, 'H');

    auto check = [&](int lim) -> bool {
        int i, j;
        for(i = 1; i <= n; i++) {
            for(j = 1; j <= n; j++) {
                M[i][j] = INF;
            }
        }
        if(lim >= H[lm][cm]) {
            return 0;
        }
        queue < pair <int, int> > Q; Q.push({lm, cm});
        M[lm][cm] = 0;
        while(Q.size()) {
            auto cur = Q.front();
            Q.pop();
            for(int i = 0; i < 4; i++) {
                int l = cur.first + dl[i], c = cur.second + dc[i];
                int d = (M[cur.first][cur.second] + 1) / s + lim;
                if(in(l, c) && M[l][c] == INF && d < H[l][c] && (mat[l][c] == 'G' || mat[l][c] == 'D')) {
                    Q.push({l, c});
                    M[l][c] = M[cur.first][cur.second] + 1;
                }
            }
        }
        return M[ld][cd] != INF;
    };

    int res = 0;
    for(int step = 1 << 19; step; step >>= 1) {
        if(check(res + step)) {
            res += step;
        }
    }
    cout << (check(res) ? res : -1);

    return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'int main()':
mecho.cpp:88:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         cin >> mat[i] + 1;
                ~~~~~~~^~~
mecho.cpp:152:24: warning: 'cd' may be used uninitialized in this function [-Wmaybe-uninitialized]
         return M[ld][cd] != INF;
                        ^
mecho.cpp:152:20: warning: 'ld' may be used uninitialized in this function [-Wmaybe-uninitialized]
         return M[ld][cd] != INF;
                    ^
#Verdict Execution timeMemoryGrader output
Fetching results...