Submission #1100001

#TimeUsernameProblemLanguageResultExecution timeMemory
1100001chaoslongMecho (IOI09_mecho)C++14
100 / 100
169 ms18076 KiB
// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <ll,ll>
#define pll pair <ll,ll>
#define piii pair <ll,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"

using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7; // 998244353
const ll oo = 1e18;

pii bd, en;
ll n, s, dx[] {1, -1, 0, 0}, dy[] {0, 0, 1, -1}, d[1005][1005], d1[1005][1005];
char a[1005][1005];
queue<piii> q;

bool check(ll x, ll y) {
    if(x > n || x < 1 || y > n || y < 1 || a[x][y] == 'T') return false;
    return true;
}

bool kt(ll mid) {
    if(mid >= d[bd.st][bd.nd]) return false;
    memset(d1, 0, sizeof d1);
    q.push({mid * s, {bd.st, bd.nd}});
    while(q.size()) {
        ll dxy = q.front().st, x = q.front().nd.st, y = q.front().nd.nd; q.pop();
        if(d1[x][y]) continue;
        d1[x][y] = 1;
        forr(i, 0, 3) {
            ll nx = x + dx[i], ny = y + dy[i];
            if(!check(nx, ny)) continue;
            if((dxy + 1) / s < d[nx][ny]) {
//                cout << nx << " " << ny << " " << dxy + 1 << " " << d[nx][ny] << " " << (dxy + 1) / s + ((dxy + 1) % s != 0) << "\n";
                q.push({dxy + 1, {nx, ny}});
            }
        }
    }
    return d1[en.st][en.nd];
}

void to_nho_cau() {
    cin >> n >> s;
    memset(d, 63, sizeof d);
    forr(i, 1, n) {
        forr(j, 1, n) {
            cin >> a[i][j];
            if(a[i][j] == 'H') {
                d[i][j] = 0;
                q.push({0, {i, j}});
            }
            if(a[i][j] == 'M') bd = {i, j};
            if(a[i][j] == 'D') en = {i, j};
        }
    }
    while(q.size()) {
        ll dxy = q.front().st, x = q.front().nd.st, y = q.front().nd.nd; q.pop();
        if(d[x][y] < dxy) continue;
        forr(i, 0, 3) {
            ll nx = x + dx[i], ny = y + dy[i];
            if(!check(nx, ny) || a[nx][ny] == 'D') continue;
            if(dxy + 1 < d[nx][ny]) {
                d[nx][ny] = dxy + 1;
                q.push({dxy + 1, {nx, ny}});
            }
        }
    }
    ll l = 0, r = mod, ans = -1;
    while(l <= r) {
        ll mid = (l + r) >> 1;
        if(kt(mid)) {
            ans = mid;
            l = mid + 1;
        } else r = mid - 1;
    }
    cout << ans << "\n";
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
//    #ifdef LOCAL
        //freopen(file".inp","r",stdin);
        //freopen(file".out","w",stdout);
//    #endif
    int t = 1;
    //cin >> t;
    while(t--) to_nho_cau();
}
#Verdict Execution timeMemoryGrader output
Fetching results...