結果

問題 No.1638 Robot Maze
ユーザー sotanishysotanishy
提出日時 2021-08-06 21:42:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 550 ms / 2,000 ms
コード長 1,690 bytes
コンパイル時間 2,182 ms
コンパイル使用メモリ 214,952 KB
最終ジャッジ日時 2025-01-23 14:53:38
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    const ll INF = 1e18;
    int H, W;
    cin >> H >> W;
    int U, D, R, L, P;
    ll K;
    cin >> U >> D >> R >> L >> K >> P;
    int xs, ys, xt, yt;
    cin >> xs >> ys >> xt >> yt;
    --xs, --ys, --xt, --yt;
    vector<string> C(H);
    for (auto& x : C) cin >> x;
    vector dist(H, vector<vector<ll>>(W, vector<ll>(H+W, INF)));
    dist[xs][ys][0] = 0;

    vector<int> dx = {-1, 1, 0, 0};
    vector<int> dy = {0, 0, 1, -1};
    vector<int> cost = {U, D, R, L};

    using T = tuple<ll, int, int, int>;
    priority_queue<T, vector<T>, greater<T>> pq;
    pq.push({0, xs, ys, 0});
    while (!pq.empty()) {
        ll d;
        int x, y, t;
        tie(d, x, y, t) = pq.top();
        pq.pop();
        if (dist[x][y][t] < d) continue;
        for (int k = 0; k < 4; ++k) {
            int nx = x + dx[k], ny = y + dy[k];
            if (0 <= nx && nx < H && 0 <= ny && ny <= W && C[nx][ny] != '#') {
                if (C[nx][ny] == '.' && dist[nx][ny][t] > d + cost[k]) {
                    dist[nx][ny][t] = d + cost[k];
                    pq.push({dist[nx][ny][t], nx, ny, t});
                }
                if (C[nx][ny] == '@' && t+1 < H+W && dist[nx][ny][t+1] > d + cost[k] + P) {
                    dist[nx][ny][t+1] = d + cost[k] + P;
                    pq.push({dist[nx][ny][t+1], nx, ny, t+1});
                }
            }
        }
    }
    for (int t = 0; t < H + W; ++t) {
        if (dist[xt][yt][t] <= K) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
}
0