Submission #1251074

#TimeUsernameProblemLanguageResultExecution timeMemory
1251074papauloMecho (IOI09_mecho)C++20
100 / 100
125 ms7496 KiB
#include <bits/stdc++.h>
using namespace std;

int n, s;
#define MAXN 880
int bb[MAXN][MAXN];
int bm[MAXN][MAXN];
char mat[MAXN][MAXN];

const int dx[4]={1, 0, -1, 0};
const int dy[4]={0, 1, 0, -1};

int test(int se) {
    memset(bm, 0x0f, sizeof(bm));
    queue<pair<int, int>> q;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            if(mat[i][j]=='M') {
                bm[i][j]=0;
                q.push({i, j});
            }
        }
    }
    while(!q.empty()) {
        auto pr=q.front();
        q.pop();
        int i=pr.first, j=pr.second;
        if(bm[i][j]/s+se>=bb[i][j]) continue;
        int newb=bm[i][j]+1;
        for(int k=0;k<4;k++) {
            int newi=i+dx[k], newj=j+dy[k];
            if(bm[newi][newj]>newb&&mat[newi][newj]=='G') {
                bm[newi][newj]=newb;
                q.push({newi, newj});
            } else if(mat[newi][newj]=='D') {
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    memset(mat, 'T', sizeof(mat));
    memset(bb, 0x0f, sizeof(bb));
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> n >> s;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            cin >> mat[i][j];
        }
    }
    queue<pair<int, int>> q;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=n;j++) {
            if(mat[i][j]=='H') {
                bb[i][j]=0;
                q.push({i, j});
            }
        }
    }
    while(!q.empty()) {
        auto pr=q.front();
        q.pop();
        int i=pr.first, j=pr.second;
        int newb=bb[i][j]+1;
        for(int k=0;k<4;k++) {
            int newi=i+dx[k], newj=j+dy[k];
            if(bb[newi][newj]>newb&&(mat[newi][newj]=='G'||mat[newi][newj]=='M')) {
                bb[newi][newj]=newb;
                q.push({newi, newj});
            }
        }
    }
    int l=-1, r=1e8;
    while(l<r) {
        int mid=(l+r+1)/2;
        if(test(mid)) l=mid;
        else r=mid-1;
    }
    cout << l << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...