Submission #395793

#TimeUsernameProblemLanguageResultExecution timeMemory
395793SirCovidThe19thMecho (IOI09_mecho)C++14
84 / 100
267 ms6388 KiB
#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;
}
bool checkDone(int x, int y){
    //check if bear is next to his home
    return (abs(x-dest.first) == 1 and y-dest.second == 0) or
           ((x-dest.first) == 0 and abs(y-dest.second) == 1);
}
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' 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;
    //floor(bearDist/s) = time
    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;

    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];
                if (inside(x, y) and grid[x][y] == 'G' 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;
                    if (checkDone(x, y)) return true;
                    //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();
    
    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 (stderr)

mecho.cpp: In function 'int main()':
mecho.cpp:98:11: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   98 |     cout<<ans;
      |           ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...