제출 #1348829

#제출 시각아이디문제언어결과실행 시간메모리
1348829kantaponzMecho (IOI09_mecho)C++20
100 / 100
128 ms12256 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long

const int nx = 805;

int n; ll s;
char grid[nx][nx];
ll dist_bee[nx][nx];
int sy, sx, dy, dx;
int xx[4] = {-1, 0, 1, 0};
int yy[4] = {0, 1, 0, -1};
vector<pair<int,int>> hives;
// T, G, M, D, H

bool check(int time) {
    queue<pair<int,int>> q;
    bool vis[nx][nx]; memset(vis, 0, sizeof vis);
    ll dist[nx][nx]; memset(dist, 0, sizeof dist);
    dist[sy][sx] = s * time;
    q.emplace(sy, sx);
    vis[sy][sx] = 1;
    while (!q.empty()) {
        auto [y, x] = q.front();
        q.pop();
        if (dist[y][x] >= dist_bee[y][x] * s) continue;
        for (int k = 0; k < 4; k++) {
            int ny = y + yy[k], nx = x + xx[k];
            if (ny < 1 || ny > n || nx < 1 || nx > n) continue;
            if (vis[ny][nx]) continue;
            if (grid[ny][nx] == 'T') continue;
            if (ny == dy && nx == dx) return 1;
            vis[ny][nx] = 1;
            dist[ny][nx] = dist[y][x] + 1;
            if (dist[ny][nx] >= dist_bee[ny][nx] * s) continue;
            q.emplace(ny, nx);
        }
    }

    return 0;
}

void compute_bee() {
    queue<tuple<int,int,int>> q;
    bool vis[nx][nx]; memset(vis, 0, sizeof vis);
    for (auto [i, j] : hives) q.emplace(0, i, j), vis[i][j] = 1;
    while (!q.empty()) {
        auto [w, y, x] = q.front();
        q.pop();
        for (int k = 0; k < 4; k++) {
            int ny = y + yy[k], nx = x + xx[k];
            if (ny < 1 || ny > n || nx < 1 || nx > n) continue;
            if (vis[ny][nx]) continue;
            if (grid[ny][nx] == 'D' || grid[ny][nx] == 'T') continue;
            vis[ny][nx] = 1;
            dist_bee[ny][nx] = w + 1;
            q.emplace(w + 1, ny, nx);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n >> s;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> grid[i][j];
            if (grid[i][j] == 'M') sy = i, sx = j;
            else if (grid[i][j] == 'D') dy = i, dx = j;
            else if (grid[i][j] == 'H') hives.emplace_back(i, j);
        }
    }

    for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dist_bee[i][j] = 1e9;

    compute_bee();

    int l = 0, r = n * n;
    while (l < r) {
        int mid = ceil(1.0 * (l + r) / 2.0);
        if (check(mid)) l = mid;
        else r = mid - 1;
    }

    if (check(l)) cout << l;
    else cout << -1;
}

/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT

1

7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGTGGD
TGGGGGT
TGGGGGT
THHHHHT

7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT

2
*/
#Verdict Execution timeMemoryGrader output
Fetching results...