Submission #1012534

#TimeUsernameProblemLanguageResultExecution timeMemory
1012534toast12Mecho (IOI09_mecho)C++14
73 / 100
106 ms6928 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 800;

int n, s;
vector<vector<char>> grid;
vector<vector<int>> dist;

int honey;

bool reachable(int mecho, int bee) {
    return (((mecho/s)+honey < bee) || (bee == -1)) && (mecho != -1); 
}

void bfs(queue<pair<int, int>> &q, vector<vector<int>> &d) {
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        if (x < 1 || y < 1 || x > n || y > n)
            continue;
        if (d[x+1][y] == -1 && grid[x+1][y] != 'X' && reachable(d[x][y]+1, dist[x+1][y])) {
            q.push({x+1, y});
            d[x+1][y] = d[x][y]+1;
        }
        if (d[x-1][y] == -1 && grid[x-1][y] != 'X' && reachable(d[x][y]+1, dist[x-1][y])) {
            q.push({x-1, y});
            d[x-1][y] = d[x][y]+1;
        }
        if (d[x][y+1] == -1 && grid[x][y+1] != 'X' && reachable(d[x][y]+1, dist[x][y+1])) {
            q.push({x, y+1});
            d[x][y+1] = d[x][y]+1;
        }
        if (d[x][y-1] == -1 && grid[x][y-1] != 'X' && reachable(d[x][y]+1, dist[x][y-1])) {
            q.push({x, y-1});
            d[x][y-1] = d[x][y]+1;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> s;
    grid.resize(n+2, vector<char>(n+2, '.'));
    pair<int, int> start, end;
    queue<pair<int, int>> q;
    dist.resize(n+2, vector<int>(n+2, -1));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> grid[i][j];
            if (grid[i][j] == 'G')
                grid[i][j] = '.';
            else if (grid[i][j] == 'T')
                grid[i][j] = 'X';
            else if (grid[i][j] == 'M')
                start = {i, j};
            else if (grid[i][j] == 'D')
                end = {i, j};
            else {
                q.push({i, j});
                dist[i][j] = 0;
            }
        }
    }
    // distance for bees
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        if (x < 1 || y < 1 || x > n || y > n)
            continue;
        if (dist[x+1][y] == -1 && grid[x+1][y] != 'X') {
            q.push({x+1, y});
            dist[x+1][y] = dist[x][y]+1;
        }
        if (dist[x-1][y] == -1 && grid[x-1][y] != 'X') {
            q.push({x-1, y});
            dist[x-1][y] = dist[x][y]+1;
        }
        if (dist[x][y+1] == -1 && grid[x][y+1] != 'X') {
            q.push({x, y+1});
            dist[x][y+1] = dist[x][y]+1;
        }
        if (dist[x][y-1] == -1 && grid[x][y-1] != 'X') {
            q.push({x, y-1});
            dist[x][y-1] = dist[x][y]+1;
        }
    }
    int lo = 0, hi = MAXN*MAXN;
    int ans = -1;
    while (lo < hi) {
        int mid = lo+(hi-lo)/2;
        vector<vector<int>> d(n+2, vector<int>(n+2, -1));
        d[start.first][start.second] = 0;
        honey = mid;
        q.push(start);
        bfs(q, d);
        if (reachable(d[end.first][end.second], dist[end.first][end.second])) {
            lo = mid+1;
            ans = mid;
        }
        else {
            hi = mid;
        }
    }
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...