제출 #470769

#제출 시각아이디문제언어결과실행 시간메모리
470769Yazan_AlattarMecho (IOI09_mecho)C++14
45 / 100
134 ms11588 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;
char a[M][M];
int n, s, d[M][M], dist[M][M], res[M][M];
bool vist[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] = 1;
            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' || d[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
            d[ny][nx] = d[y][x] + 1;
            q.push({ny, nx});
        }
    }
//    for(int i = 1; i <= n; ++i){
//        for(int j = 1; j <= n; ++j){
//            cout << d[i][j] << " ";
//        }
//        cout << endl;
//    }
    q.push(st);
    dist[st.F][st.S] = 1;
    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' || (dist[y][x]) / s >= d[ny][nx] - 1 || dist[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
            dist[ny][nx] = dist[y][x] + 1;
            q.push({ny, nx});
        }
    }
//    cout << endl;
//    for(int i = 1; i <= n; ++i){
//        for(int j = 1; j <= n; ++j) cout << dist[i][j] << " ";
//        cout << endl;
//    }
    if(!dist[en.F][en.S]) return cout << -1 << endl, 0;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            res[i][j] = -1;
            if(d[i][j] && dist[i][j]) res[i][j] = (d[i][j] - 1) - ((dist[i][j] - 2) / s + 1);
//            cout << res[i][j] << " ";
        }
//        cout << endl;
    }
    priority_queue < pair < int, pair <int,int> > > pq;
    pq.push({res[st.F][st.S], st});
    vist[st.F][st.S] = 1;
    while(!pq.empty()){
        int x = pq.top().S.S;
        int y = pq.top().S.F;
        int cost = pq.top().F;
        pq.pop();
//        cout << x << " " << y << " " << cost << endl;
        if(x == en.S && y == en.F) return cout << cost << endl, 0;
        for(int i = 0; i < 4; ++i){
            int nx = x + dx[i];
            int ny = y + dy[i];
            int ncost = min(cost, res[ny][nx]);
            if(vist[ny][nx] || dist[y][x] >= dist[ny][nx] || ncost == -1 || ny < 1 || nx < 1 || ny > n || nx > n) continue;
            vist[ny][nx] = 1;
            pq.push({ncost, {ny, nx}});
        }
    }
    return 0;
}
// Don't forget special cases. (n = 1?)
// Look for the constraints. (Runtime array? overflow?)
#Verdict Execution timeMemoryGrader output
Fetching results...