Submission #395795

# Submission time Handle Problem Language Result Execution time Memory
395795 2021-04-28T23:37:51 Z SirCovidThe19th Mecho (IOI09_mecho) C++14
90 / 100
223 ms 6912 KB
#include <bits/stdc++.h>
using namespace std;

int n, s;
int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, -1, 1};
int hiveDist[800][800];
char grid[800][800];

pair<int, int> start;
pair<int, int> dest;
vector<pair<int, int>> hives;

bool inside(int x, int y){
    return x >= 0 and x < n and y >= 0 and y < n;
}
//precompute how far every cell is to the nearest hive
void bees(){
    queue<pair<int, int>> trav;
    fill(&hiveDist[0][0], &hiveDist[0][0]+sizeof(hiveDist)/sizeof(hiveDist[0][0]), -1);
    for (pair<int, int> loc : hives){
        trav.push(loc); 
        hiveDist[loc.first][loc.second] = 0;
    }
    while (!trav.empty()){
        pair<int, int> curr = trav.front(); trav.pop();
        for (int i = 0; i < 4; i++){
            int x = curr.first+dx[i]; int y = curr.second+dy[i];
            if (inside(x, y) and (grid[x][y] == 'G' or grid[x][y] == 'M') and hiveDist[x][y] == -1){
                hiveDist[x][y] = hiveDist[curr.first][curr.second]+1;
                trav.push({x, y});
            }
        }
    }
}
bool bear(int wait){
    queue<pair<int, int>> trav;
    //time it takes for bees is floor(bearDist/s)
    int bearDist[n][n];
    fill(&bearDist[0][0], &bearDist[0][0]+sizeof(bearDist)/sizeof(bearDist[0][0]), -1);
    trav.push(start); bearDist[start.first][start.second] = wait*s;
    //check if bees can reach mecho before he even moves
    if (wait >= hiveDist[start.first][start.second]) return false;
    while (!trav.empty()){
        pair<int, int> curr = trav.front(); trav.pop();
        //smoll bfs on reachable positions
        queue<pair<int, int>> pos; pos.push(curr);
        while (!pos.empty()){
            pair<int, int> loc = pos.front(); pos.pop();
            for (int i = 0; i < 4; i++){
                int x = loc.first+dx[i]; int y = loc.second+dy[i];
                //ok, mecho reached home
                if (grid[x][y] == 'D') return true;
                if (inside(x, y) and (grid[x][y] == 'G' or grid[x][y] == 'D') and bearDist[x][y] == -1){
                    bearDist[x][y] = bearDist[loc.first][loc.second]+1;
                    //uh oh, bees can reach here before you can
                    if (floor(bearDist[x][y]/s) >= hiveDist[x][y] and hiveDist[x][y] != -1) continue;
                    //max distance reached
                    int maxDist = (bearDist[curr.first][curr.second]/s)+1;
                    if (bearDist[x][y] == maxDist)
                        trav.push({x, y});
                    else
                        pos.push({x, y});
                }
            }
        }
    }
    return false;
}
int main() {
    cin >> n >> s;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            cin >> grid[i][j];
            if (grid[i][j] == 'M') start = {i, j};
            if (grid[i][j] == 'D') dest = {i, j};
            if (grid[i][j] == 'H') hives.push_back({i, j});
        }
    }
    bees();
    //impossible
    if (!bear(0)){
        cout<<-1; return 0;
    }
    int high = n*n; int low = 0; int ans;
    while (low < high){
        int mid = (low+high)/2;
        if (bear(mid)){
            ans = mid; low = mid+1;
        }
        else 
            high = mid;
    }
    cout<<ans;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:93:11: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   93 |     cout<<ans;
      |           ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 2 ms 2764 KB Output is correct
3 Correct 3 ms 2764 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 2 ms 2740 KB Output is correct
7 Incorrect 130 ms 6564 KB Output isn't correct
8 Correct 2 ms 2764 KB Output is correct
9 Correct 2 ms 2768 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 3 ms 2764 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 2 ms 2764 KB Output is correct
16 Correct 2 ms 2764 KB Output is correct
17 Correct 2 ms 2764 KB Output is correct
18 Correct 2 ms 2768 KB Output is correct
19 Correct 3 ms 2736 KB Output is correct
20 Correct 2 ms 2764 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 3 ms 2768 KB Output is correct
25 Correct 2 ms 2764 KB Output is correct
26 Correct 2 ms 2868 KB Output is correct
27 Correct 2 ms 2764 KB Output is correct
28 Correct 2 ms 2764 KB Output is correct
29 Correct 2 ms 2764 KB Output is correct
30 Correct 3 ms 2764 KB Output is correct
31 Correct 3 ms 2896 KB Output is correct
32 Correct 3 ms 2764 KB Output is correct
33 Correct 12 ms 3668 KB Output is correct
34 Correct 13 ms 3660 KB Output is correct
35 Correct 36 ms 3660 KB Output is correct
36 Correct 15 ms 3788 KB Output is correct
37 Correct 16 ms 3896 KB Output is correct
38 Correct 50 ms 3792 KB Output is correct
39 Correct 19 ms 4072 KB Output is correct
40 Correct 19 ms 4084 KB Output is correct
41 Correct 61 ms 4108 KB Output is correct
42 Correct 22 ms 4416 KB Output is correct
43 Correct 23 ms 4308 KB Output is correct
44 Correct 92 ms 4408 KB Output is correct
45 Correct 27 ms 4680 KB Output is correct
46 Correct 28 ms 4716 KB Output is correct
47 Correct 94 ms 4700 KB Output is correct
48 Correct 31 ms 5004 KB Output is correct
49 Correct 32 ms 4900 KB Output is correct
50 Correct 103 ms 5016 KB Output is correct
51 Correct 37 ms 5364 KB Output is correct
52 Correct 38 ms 5436 KB Output is correct
53 Correct 129 ms 5304 KB Output is correct
54 Correct 43 ms 5732 KB Output is correct
55 Correct 42 ms 5612 KB Output is correct
56 Correct 173 ms 5832 KB Output is correct
57 Correct 54 ms 6012 KB Output is correct
58 Correct 50 ms 6084 KB Output is correct
59 Correct 175 ms 6128 KB Output is correct
60 Correct 59 ms 6468 KB Output is correct
61 Incorrect 55 ms 6596 KB Output isn't correct
62 Correct 223 ms 6548 KB Output is correct
63 Correct 178 ms 6468 KB Output is correct
64 Incorrect 123 ms 6552 KB Output isn't correct
65 Incorrect 122 ms 6544 KB Output isn't correct
66 Correct 192 ms 6664 KB Output is correct
67 Incorrect 114 ms 6596 KB Output isn't correct
68 Correct 100 ms 6536 KB Output is correct
69 Correct 90 ms 6452 KB Output is correct
70 Correct 87 ms 6512 KB Output is correct
71 Correct 93 ms 6500 KB Output is correct
72 Correct 86 ms 6532 KB Output is correct
73 Correct 84 ms 6752 KB Output is correct
74 Correct 129 ms 6908 KB Output is correct
75 Correct 133 ms 6912 KB Output is correct
76 Correct 124 ms 6852 KB Output is correct
77 Correct 128 ms 6848 KB Output is correct
78 Correct 73 ms 6836 KB Output is correct
79 Correct 114 ms 6796 KB Output is correct
80 Correct 122 ms 6864 KB Output is correct
81 Correct 138 ms 6712 KB Output is correct
82 Correct 124 ms 6700 KB Output is correct
83 Correct 157 ms 6736 KB Output is correct
84 Correct 157 ms 6684 KB Output is correct
85 Correct 135 ms 6720 KB Output is correct
86 Correct 138 ms 6712 KB Output is correct
87 Correct 145 ms 6828 KB Output is correct
88 Correct 209 ms 6724 KB Output is correct
89 Correct 173 ms 6716 KB Output is correct
90 Correct 155 ms 6688 KB Output is correct
91 Correct 165 ms 6608 KB Output is correct
92 Correct 145 ms 6596 KB Output is correct