結果

問題 No.3504 Insert Maze
コンテスト
ユーザー kuma-1971
提出日時 2026-04-18 04:24:36
言語 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,154 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,354 ms
コンパイル使用メモリ 174,652 KB
実行使用メモリ 414,384 KB
最終ジャッジ日時 2026-04-18 04:25:21
合計ジャッジ時間 7,322 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 8 TLE * 1 -- * 67
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <string>
#include <deque>

using namespace std;

const int INF = 1e9;

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

    vector<vector<int>> dist(H, vector<int>(W, INF));
    deque<pair<int, int>> dq;

    dist[0][0] = 0;
    dq.push_back({0, 0});

    int dy[] = {1, -1, 0, 0};
    int dx[] = {0, 0, 1, -1};

    while (!dq.empty()) {
        pair<int, int> cur = dq.front();
        dq.pop_front();
        int y = cur.first;
        int x = cur.second;

        for (int i = 0; i < 4; ++i) {
            int ny = y + dy[i];
            int nx = x + dx[i];

            if (ny >= 0 && ny < H && nx >= 0 && nx < W) {
                int cost = (C[ny][nx] == '#' ? 2 : 1);
                if (dist[ny][nx] > dist[y][x] + cost) {
                    dist[ny][nx] = dist[y][x] + cost;
                    if (cost == 1) dq.push_front({ny, nx});
                    else dq.push_back({ny, nx});
                }
            }
        }
    }

    int res = dist[H - 1][W - 1];
    cout << (res == INF ? -1 : res) << endl;

    return 0;
}
0