結果

問題 No.3504 Insert Maze
コンテスト
ユーザー Enderaoe Lyther
提出日時 2026-04-17 21:32:48
言語 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
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 1,386 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,370 ms
コンパイル使用メモリ 173,228 KB
実行使用メモリ 70,272 KB
最終ジャッジ日時 2026-04-17 21:33:03
合計ジャッジ時間 12,274 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

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

    int RH = 2 * H - 1;
    int CW = 2 * W - 1;
    const int INF = 1e9;

    auto passable = [&](int r, int c) {
        if ((r & 1) == 0 && (c & 1) == 0) {
            return C[r / 2][c / 2] != '#';
        }
        return true;
    };

    vector<vector<int>> dp(RH, vector<int>(CW, INF));
    dp[0][0] = 0;

    for (int r = 0; r < RH; ++r) {
        for (int c = 0; c < CW; ++c) {
            if (dp[r][c] == INF || !passable(r, c)) continue;

            if (c + 1 < CW && passable(r, c + 1)) {
                dp[r][c + 1] = min(dp[r][c + 1], dp[r][c] + 1);
            }
            if ((c & 1) == 0 && c + 2 < CW && passable(r, c + 2)) {
                dp[r][c + 2] = min(dp[r][c + 2], dp[r][c] + 1);
            }
            if (r + 1 < RH && passable(r + 1, c)) {
                dp[r + 1][c] = min(dp[r + 1][c], dp[r][c] + 1);
            }
            if ((r & 1) == 0 && r + 2 < RH && passable(r + 2, c)) {
                dp[r + 2][c] = min(dp[r + 2][c], dp[r][c] + 1);
            }
        }
    }

    int ans = dp[RH - 1][CW - 1];
    cout << (ans == INF ? -1 : ans) << '\n';
    return 0;
}
0