Submission #470811

#TimeUsernameProblemLanguageResultExecution timeMemory
470811Yazan_AlattarMecho (IOI09_mecho)C++14
100 / 100
209 ms8568 KiB
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 1007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

queue < pair <int,int> > q;
pair <int,int> st, en;
int n, s, d[M][M], dist[M][M];
char a[M][M];

int main()
{
//    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> s;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            cin >> a[i][j];
            if(a[i][j] == 'H') q.push({i, j}), d[i][j] = s;
            if(a[i][j] == 'D') en = {i, j};
            if(a[i][j] == 'M') st = {i, j};
        }
    }
    while(!q.empty()){
        int x = q.front().S;
        int y = q.front().F;
        q.pop();
        for(int i = 0; i < 4; ++i){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(a[ny][nx] == 'H' || a[ny][nx] == 'T' || a[ny][nx] == 'D' || d[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
            d[ny][nx] = d[y][x] + s;
            q.push({ny, nx});
        }
    }
    d[en.F][en.S] = 2e9;
    int l = 0, r = 1e6;
    while(l < r){
        memset(dist, 0, sizeof dist);
        int mid = (l + r) / 2;
        q.push(st);
        dist[st.F][st.S] = mid * s + 1;
        if(dist[st.F][st.S] - 1 >= d[st.F][st.S] - s) q.pop();
        while(!q.empty()){
            int x = q.front().S;
            int y = q.front().F;
            q.pop();
            for(int i = 0; i < 4; ++i){
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(a[ny][nx] == 'T' || dist[ny][nx] || dist[y][x] >= d[ny][nx] - s || ny > n || nx > n || ny < 1 || nx < 1) continue;
                dist[ny][nx] = dist[y][x] + 1;
                q.push({ny, nx});
            }
        }
        if(dist[en.F][en.S]) l = mid + 1;
        else r = mid;
    }
    cout << l - 1 << endl;
    return 0;
}
// Don't forget special cases. (n = 1?)
// Look for the constraints. (Runtime array? overflow?)
#Verdict Execution timeMemoryGrader output
Fetching results...