제출 #65308

#제출 시각아이디문제언어결과실행 시간메모리
65308zubecMecho (IOI09_mecho)C++14
100 / 100
282 ms7452 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

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

int n, sx, sy, fx, fy, dB[810][810], d[810][810], S;

char a[810][810];

bool f(int bound){
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++)
            d[i][j] = 1e9;
    }
    d[sx][sy] = 0;
    queue <pair<int, int> > q;
    q.push({sx, sy});
    while(!q.empty()){
        int x = q.front().first, y = q.front().second;
        q.pop();
        int curTime = bound + max(0, (d[x][y]))/S;
        if (curTime >= dB[x][y])
            continue;
        for (int i = 0; i < 4; i++){
            int xx = x + dx[i], yy = y + dy[i];
            if (xx < 1 || yy < 1 || xx > n || yy > n || a[xx][yy] == 'T' || curTime >= dB[xx][yy])
                continue;
            if (d[x][y] + 1 < d[xx][yy]){
                d[xx][yy] = d[x][y] + 1;
                q.push({xx, yy});
            }
        }
    }
    return d[fx][fy] != 1e9;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    cin >> n >> S;
    queue <pair<int, int> > q;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            cin >> a[i][j];
            dB[i][j] = 1e9;
            if (a[i][j] == 'H'){
                q.push({i, j});
                dB[i][j] = 0;
            }
            if (a[i][j] == 'M'){
                sx = i; sy = j;
            }
            if (a[i][j] == 'D'){
                fx = i; fy = j;
            }
        }
    }
    while(!q.empty()){
        int x = q.front().first, y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++){
            int xx = x + dx[i], yy = y + dy[i];
            if (xx < 1 || yy < 1 || xx > n || yy > n || a[xx][yy] == 'T' || a[xx][yy] == 'D')
                continue;
            if (dB[x][y] + 1 < dB[xx][yy]){
                dB[xx][yy] = dB[x][y] + 1;
                q.push({xx, yy});
            }
        }
    }
    int l = 0, r = 2000000;
    while(l < r){
        int mid = (l+r+1)>>1;
        if (f(mid))
            l = mid; else
            r = mid-1;
    }
    if (f(l) == 0)
        l = -1;
    cout << l;

}

/**

4 3
TTDT
THGT
TTGT
TTGM

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