제출 #221433

#제출 시각아이디문제언어결과실행 시간메모리
221433tatyamMecho (IOI09_mecho)C++17
72 / 100
1093 ms9848 KiB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int INF = 0x3fffffff;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
#define name2(a,b,c,...) c
#define rep1(b) for(int i = 0; i < b; i++)
#define rep2(i,b) for(int i = 0; i < b; i++)
#define rep(...) name2(__VA_ARGS__,rep2,rep1)(__VA_ARGS__)
#define each(i,a) for(auto&& i : a)
template<class T, class U> bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; }


int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, s;
    cin >> n >> s;
    vector<string> a(n);
    each(i, a) cin >> i;
    vector<vector<int>> cost(n, vector<int>(n, INF));
    queue<pii> q;
    rep(n) rep(j, n) if(a[i][j] == 'H'){
        cost[i][j] = 0;
        q.emplace(i, j);
    }
    while(q.size()){
        auto [x, y] = q.front();
        q.pop();
        rep(i, 4){
            int x2 = x + dx[i], y2 = y + dy[i];
            if(x2 < 0 || y2 < 0 || x2 >= n || y2 >= n || a[x2][y2] != 'G') continue;
            if(chmin(cost[x2][y2], cost[x][y] + 1)) q.emplace(x2, y2);
        }
    }
    vector<vector<pii>> ans(n, vector<pii>(n));
    rep(n) rep(j, n) if(a[i][j] == 'M'){
        ans[i][j].first = cost[i][j];
        q.emplace(i, j);
    }
    while(q.size()){
        auto [x, y] = q.front();
        q.pop();
        rep(i, 4){
            int x2 = x + dx[i], y2 = y + dy[i];
            if(x2 < 0 || y2 < 0 || x2 >= n || y2 >= n || a[x2][y2] == 'T') continue;
            pii a = ans[x][y];
            a.second--;
            chmin(a.first, cost[x2][y2] + a.second / s);
            if(chmax(ans[x2][y2], a)) q.emplace(x2, y2);
        }
    }
    rep(n) rep(j, n) if(a[i][j] == 'D') cout << ans[i][j].first - 1 << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...