結果

問題 No.2913 二次元距離空間
ユーザー Today03
提出日時 2024-10-04 22:02:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,382 bytes
コンパイル時間 2,870 ms
コンパイル使用メモリ 261,572 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-10-04 22:02:18
合計ジャッジ時間 3,937 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    for (int i = 0; i < H; i++) cin >> S[i];

    using P = pair<int, int>;
    vector<vector<P>> dst(H, vector<P>(W, P(INF, INF)));
    priority_queue<pair<P, P>, vector<pair<P, P>>, greater<pair<P, P>>> pq;
    pq.push({P(0, 0), P(0, 0)});
    while (!pq.empty()) {
        auto [horz, vert] = pq.top().first;
        auto [x, y] = pq.top().second;
        pq.pop();
        if (dst[x][y] < P(horz, vert)) continue;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if (S[nx][ny] == '#') continue;
            int nvert = vert, nhorz = horz;
            if (x != nx) nvert++;
            if (y != ny) nhorz++;
            if (dst[nx][ny] > P(nhorz, nvert)) {
                dst[nx][ny] = P(nhorz, nvert);
                pq.push({P(nhorz, nvert), P(nx, ny)});
            }
        }
    }

    if (dst.back().back() == P(INF, INF)) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
        cout << dst.back().back().first << ' ' << dst.back().back().second << endl;
    }
}
0