結果

問題 No.3504 Insert Maze
コンテスト
ユーザー kuma-1971
提出日時 2026-04-18 04:34:40
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,238 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,298 ms
コンパイル使用メモリ 189,164 KB
実行使用メモリ 26,880 KB
最終ジャッジ日時 2026-04-18 04:35:23
合計ジャッジ時間 29,417 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21 WA * 64
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <string>
#include <queue>

using namespace std;

int main() {
    int H, W;
    if (!(cin >> H >> W)) return 0;

    vector<string> C(H);
    for (int i = 0; i < H; ++i) cin >> C[i];

    vector<vector<int>> dist(H, vector<int>(W, 2e9));
    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;

    dist[0][0] = 0;
    pq.push({0, {0, 0}});

    int dr[] = {0, 0, 1, -1};
    int dc[] = {1, -1, 0, 0};

    while (!pq.empty()) {
        int d = pq.top().first;
        int r = pq.top().second.first;
        int c = pq.top().second.second;
        pq.pop();

        if (d > dist[r][c]) continue;

        for (int i = 0; i < 4; ++i) {
            int nr = r + dr[i];
            int nc = c + dc[i];

            if (nr >= 0 && nr < H && nc >= 0 && nc < W) {
                int cost = (C[nr][nc] == '#') ? 2 : 1;
                if (dist[nr][nc] > d + cost) {
                    dist[nr][nc] = d + cost;
                    pq.push({dist[nr][nc], {nr, nc}});
                }
            }
        }
    }

    if (dist[H - 1][W - 1] == 2e9) cout << -1 << endl;
    else cout << dist[H - 1][W - 1] << endl;

    return 0;
}
0