제출 #542410

#제출 시각아이디문제언어결과실행 시간메모리
542410ddy888Mecho (IOI09_mecho)C++17
100 / 100
221 ms11772 KiB
#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long 
#define pb push_back
#define fi first
#define si second
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;  
template<typename T> bool chmin(T &a, T b) {return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b) {return (b > a) ? a = b, 1 : 0;}
const int INF = 1e9;

int N, S;
char A[810][810];
int dist[810][810], rev[810][810];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
queue<pi> q;
pi st;

bool inside(int x, int y) {return (x >= 1 && x <= N && y >= 1 && y <= N);}

bool good(int delta) {
    while (q.size()) q.pop();
    memset(rev, -1, sizeof rev);
    rev[st.fi][st.si] = 0;
    q.push(st);
    while (q.size()) {
        auto [x, y] = q.front(); q.pop();
        if (rev[x][y] / S + delta >= dist[x][y]) continue;
        if (A[x][y] == 'D') return 1;
        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (A[nx][ny] == 'T') continue;
            if (!inside(nx, ny)) continue;
            if (rev[nx][ny] != -1) continue;
            rev[nx][ny] = rev[x][y] + 1;
            q.push({nx, ny});
        }
    }
    return 0;
}

signed main() {
    fast;
    cin >> N >> S;
    for (int i = 1; i <= N; ++i) {
        for (int j = 1; j <= N; ++j) {
            cin >> A[i][j];
            dist[i][j] = INF;
            if (A[i][j] == 'H') {
                q.push({i, j});
                dist[i][j] = 0;
            } else if (A[i][j] == 'M') st = pi(i, j);
        }
    }
    while (q.size()) {
        auto [x, y] = q.front(); q.pop();
        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (A[nx][ny] == 'T' || A[nx][ny] == 'D') continue;
            if (!inside(nx, ny)) continue;
            if (dist[nx][ny] != INF) continue;
            dist[nx][ny] = dist[x][y] + 1;
            q.push({nx, ny});
        }
    }
    if (!good(0)) return cout << -1, 0;
    int lo = -1, hi = 1e9 + 1;
    while (lo + 1 < hi) {
        int mid = (lo + hi) / 2;
        if (good(mid)) lo = mid;
        else hi = mid;
    }
    cout << lo;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...