結果

問題 No.3504 Insert Maze
コンテスト
ユーザー GOTKAKO
提出日時 2026-04-17 22:05:59
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,069 ms / 2,000 ms
コード長 1,057 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,632 ms
コンパイル使用メモリ 229,684 KB
実行使用メモリ 226,560 KB
最終ジャッジ日時 2026-04-17 22:06:39
合計ジャッジ時間 37,728 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

    int H,W; cin >> H >> W;
    vector<string> S(H);
    for(auto &s : S) cin >> s;

    vector<pair<int,int>> dxy = {{-1,0},{0,1},{1,0},{0,-1}};
    const int inf = 1001001001;
    vector<vector<vector<int>>> dist(H,vector(W,vector<int>(3,inf)));
    queue<tuple<int,int,int>> Q;
    dist.at(0).at(0).at(0) = 0,Q.push({0,0,0});
    auto check = [&](int x,int y,int d,int v) -> void {
        if(dist.at(x).at(y).at(d) == inf) dist.at(x).at(y).at(d) = v,Q.push({x,y,d}); 
    };
    while(Q.size()){
        auto [x,y,d] = Q.front(); Q.pop();
        int now = dist.at(x).at(y).at(d)+1;
        if(x < H-1) check(x,y,1,now);
        if(y < W-1) check(x,y,2,now);
        if(x < H-1 && (S.at(x+1).at(y) != '#' || d == 1)) check(x+1,y,(d==1?1:0),now);
        if(y < W-1 && (S.at(x).at(y+1) != '#' || d == 2)) check(x,y+1,(d==2?2:0),now);
    }

    cout << *min_element(dist.at(H-1).at(W-1).begin(),dist.at(H-1).at(W-1).end()) << endl;
}
0