Submission #587485

# Submission time Handle Problem Language Result Execution time Memory
587485 2022-07-02T01:10:57 Z Agnimandur Mecho (IOI09_mecho) C++17
78 / 100
370 ms 7112 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 = 64000;
    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 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 284 ms 6880 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 360 KB Output is correct
17 Correct 1 ms 320 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 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 324 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 2 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 2 ms 332 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 36 ms 1740 KB Output is correct
34 Correct 27 ms 1700 KB Output is correct
35 Correct 36 ms 1716 KB Output is correct
36 Incorrect 37 ms 2076 KB Output isn't correct
37 Correct 34 ms 2012 KB Output is correct
38 Correct 44 ms 2004 KB Output is correct
39 Incorrect 50 ms 2492 KB Output isn't correct
40 Correct 60 ms 2620 KB Output is correct
41 Correct 67 ms 2528 KB Output is correct
42 Incorrect 63 ms 3012 KB Output isn't correct
43 Correct 56 ms 2956 KB Output is correct
44 Correct 69 ms 2956 KB Output is correct
45 Incorrect 75 ms 3464 KB Output isn't correct
46 Incorrect 66 ms 3456 KB Output isn't correct
47 Correct 105 ms 3484 KB Output is correct
48 Incorrect 101 ms 4044 KB Output isn't correct
49 Incorrect 82 ms 4032 KB Output isn't correct
50 Correct 107 ms 4016 KB Output is correct
51 Incorrect 108 ms 4620 KB Output isn't correct
52 Incorrect 105 ms 4624 KB Output isn't correct
53 Correct 122 ms 4728 KB Output is correct
54 Incorrect 120 ms 5272 KB Output isn't correct
55 Incorrect 127 ms 5276 KB Output isn't correct
56 Correct 176 ms 5348 KB Output is correct
57 Incorrect 141 ms 5956 KB Output isn't correct
58 Incorrect 147 ms 5944 KB Output isn't correct
59 Correct 195 ms 6008 KB Output is correct
60 Incorrect 176 ms 6692 KB Output isn't correct
61 Incorrect 157 ms 6672 KB Output isn't correct
62 Correct 210 ms 6708 KB Output is correct
63 Correct 263 ms 6680 KB Output is correct
64 Correct 279 ms 6668 KB Output is correct
65 Correct 302 ms 6716 KB Output is correct
66 Correct 326 ms 6704 KB Output is correct
67 Correct 19 ms 6612 KB Output is correct
68 Correct 282 ms 6612 KB Output is correct
69 Correct 231 ms 6596 KB Output is correct
70 Correct 301 ms 6696 KB Output is correct
71 Correct 269 ms 6692 KB Output is correct
72 Correct 334 ms 6688 KB Output is correct
73 Correct 213 ms 6972 KB Output is correct
74 Correct 285 ms 6988 KB Output is correct
75 Correct 313 ms 6984 KB Output is correct
76 Correct 273 ms 6964 KB Output is correct
77 Correct 296 ms 7112 KB Output is correct
78 Correct 21 ms 6924 KB Output is correct
79 Correct 276 ms 7092 KB Output is correct
80 Correct 299 ms 6992 KB Output is correct
81 Correct 370 ms 6928 KB Output is correct
82 Correct 284 ms 6988 KB Output is correct
83 Correct 284 ms 6904 KB Output is correct
84 Correct 299 ms 7008 KB Output is correct
85 Correct 287 ms 7060 KB Output is correct
86 Correct 347 ms 6892 KB Output is correct
87 Correct 294 ms 7032 KB Output is correct
88 Correct 269 ms 6756 KB Output is correct
89 Correct 267 ms 6900 KB Output is correct
90 Correct 277 ms 6980 KB Output is correct
91 Correct 272 ms 6832 KB Output is correct
92 Correct 288 ms 6832 KB Output is correct