Submission #587486

# Submission time Handle Problem Language Result Execution time Memory
587486 2022-07-02T01:14:08 Z Agnimandur Mecho (IOI09_mecho) C++17
100 / 100
359 ms 6356 KB
#include <bits/stdc++.h>
 
#define ll long long
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define si set<int>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vl vector<long long>
#define pii pair<int, int>
#define pll pair<ll,ll>
#define add push_back
#define REP(i,a) for (int i = 0; i < (a); i++)
using namespace std;
 
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
const int INF = 1005000000; 
//const ll MOD = 1000000007LL;
const ll MOD = 998244353LL;
 
int ni() {
    int x; cin >> x;
    return x;
}
 
ll nl() {
    ll x; cin >> x;
    return x;
}
 
double nd() {
    double x; cin >> x;
    return x;
}

string next() {
    string x; cin >> x;
    return x;
}

int N,S;
char grid[800][800];
pii mecho1;
pii mecho2;
vector<pii> hives;
vvi dirs = {{-1,0},{1,0},{0,-1},{0,1}};

void printDist(vvi dist) {
    REP(i,N) REP(j,N) {
        if (dist[i][j]==INF)
            cout << "I";
        else
            cout << dist[i][j];
        if (j==N-1) cout << '\n';
    }
}

bool solve(int headstart) {
    vvi mechoDist(N,vi(N,INF));
    queue<pii> mechoQ;
    mechoDist[mecho1.first][mecho1.second] = 0;
    mechoQ.push(mecho1);

    vvi beeDist(N,vi(N,INF));
    queue<pii> beeQ;
    for (pii& hive: hives) {
        beeDist[hive.first][hive.second] = 0;
        beeQ.push(hive);
    }
    while (!beeQ.empty()) {
        pii p = beeQ.front();
        if (beeDist[p.first][p.second] == headstart) break;
        beeQ.pop();
        for (vi& dir: dirs) {
            int newR = p.first+dir[0];
            int newC = p.second+dir[1];
            if (newR >= 0 && newR < N && newC >= 0 && newC < N && beeDist[newR][newC]==INF && grid[newR][newC] != 'T' && grid[newR][newC] != 'D') {
                beeDist[newR][newC] = beeDist[p.first][p.second]+1;
                beeQ.push({newR,newC});
            }
        }
    }

    int travel = 0;
    while (!mechoQ.empty()) {
        travel += S;
        //mecho moves up to travel spaces from the origin.
        while (!mechoQ.empty()) {
            pii p = mechoQ.front();
            if (mechoDist[p.first][p.second] == travel) break;
            mechoQ.pop();
            if (beeDist[p.first][p.second] < INF) {
                //bees control this cell already
                continue;
            }
            for (vi& dir: dirs) {
                int newR = p.first+dir[0];
                int newC = p.second+dir[1];
                if (newR >= 0 && newR < N && newC >= 0 && newC < N && mechoDist[newR][newC]==INF && grid[newR][newC] != 'T' && grid[newR][newC] != 'H' && beeDist[newR][newC] == INF) {
                    mechoDist[newR][newC] = mechoDist[p.first][p.second]+1;
                    mechoQ.push({newR,newC});
                }
            }
        }

        headstart++;
        while (!beeQ.empty()) {
            pii p = beeQ.front();
            if (beeDist[p.first][p.second] == headstart) break;
            beeQ.pop();
            for (vi& dir: dirs) {
                int newR = p.first+dir[0];
                int newC = p.second+dir[1];
                if (newR >= 0 && newR < N && newC >= 0 && newC < N && beeDist[newR][newC]==INF && grid[newR][newC] != 'T' && grid[newR][newC] != 'D') {
                    beeDist[newR][newC] = beeDist[p.first][p.second]+1;
                    beeQ.push({newR,newC});
                }
            }
        }
    }

    if (mechoDist[mecho2.first][mecho2.second] < INF) {
        //he can reach his home!
        return true;
    } else {
        return false;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    N = ni();
    S = ni();
    REP(i,N) {
        string s = next();
        REP(j,N) {
            grid[i][j] = s[j];
            if (grid[i][j]=='M')
                mecho1 = {i,j};
            else if (grid[i][j]=='D')
                mecho2 = {i,j};
            else if (grid[i][j]=='H')
                hives.add({i,j});
        }
    }

    int lo = 0;
    int hi = N*N;
    if (!solve(0)) {
        //mecho can't reach the house ever
        cout << -1;
        return 0;
    }
    while (lo < hi) {
        int m = (lo+hi+1)/2;
        if (solve(m)) {
            lo = m;
        } else {
            hi = m-1;
        }
    }
    cout << lo;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 232 KB Output is correct
7 Correct 304 ms 6160 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 30 ms 1580 KB Output is correct
34 Correct 28 ms 1584 KB Output is correct
35 Correct 37 ms 1592 KB Output is correct
36 Correct 39 ms 1920 KB Output is correct
37 Correct 41 ms 1952 KB Output is correct
38 Correct 47 ms 1940 KB Output is correct
39 Correct 52 ms 2332 KB Output is correct
40 Correct 50 ms 2324 KB Output is correct
41 Correct 65 ms 2300 KB Output is correct
42 Correct 69 ms 2796 KB Output is correct
43 Correct 58 ms 2712 KB Output is correct
44 Correct 79 ms 2844 KB Output is correct
45 Correct 83 ms 3180 KB Output is correct
46 Correct 73 ms 3204 KB Output is correct
47 Correct 96 ms 3212 KB Output is correct
48 Correct 98 ms 3820 KB Output is correct
49 Correct 93 ms 3704 KB Output is correct
50 Correct 113 ms 3668 KB Output is correct
51 Correct 120 ms 4324 KB Output is correct
52 Correct 114 ms 4228 KB Output is correct
53 Correct 139 ms 4204 KB Output is correct
54 Correct 140 ms 4808 KB Output is correct
55 Correct 130 ms 4796 KB Output is correct
56 Correct 180 ms 4804 KB Output is correct
57 Correct 158 ms 5416 KB Output is correct
58 Correct 151 ms 5408 KB Output is correct
59 Correct 193 ms 5404 KB Output is correct
60 Correct 184 ms 6040 KB Output is correct
61 Correct 182 ms 6068 KB Output is correct
62 Correct 227 ms 6060 KB Output is correct
63 Correct 332 ms 6176 KB Output is correct
64 Correct 354 ms 6044 KB Output is correct
65 Correct 303 ms 6040 KB Output is correct
66 Correct 350 ms 6052 KB Output is correct
67 Correct 18 ms 5972 KB Output is correct
68 Correct 344 ms 6064 KB Output is correct
69 Correct 284 ms 6092 KB Output is correct
70 Correct 349 ms 6060 KB Output is correct
71 Correct 357 ms 6064 KB Output is correct
72 Correct 359 ms 6056 KB Output is correct
73 Correct 255 ms 6344 KB Output is correct
74 Correct 306 ms 6332 KB Output is correct
75 Correct 325 ms 6352 KB Output is correct
76 Correct 337 ms 6348 KB Output is correct
77 Correct 332 ms 6356 KB Output is correct
78 Correct 19 ms 6228 KB Output is correct
79 Correct 316 ms 6324 KB Output is correct
80 Correct 318 ms 6228 KB Output is correct
81 Correct 334 ms 6296 KB Output is correct
82 Correct 338 ms 6292 KB Output is correct
83 Correct 327 ms 6244 KB Output is correct
84 Correct 343 ms 6252 KB Output is correct
85 Correct 343 ms 6248 KB Output is correct
86 Correct 339 ms 6228 KB Output is correct
87 Correct 320 ms 6248 KB Output is correct
88 Correct 314 ms 6212 KB Output is correct
89 Correct 347 ms 6300 KB Output is correct
90 Correct 332 ms 6328 KB Output is correct
91 Correct 315 ms 6200 KB Output is correct
92 Correct 312 ms 6328 KB Output is correct