結果

問題 No.3504 Insert Maze
コンテスト
ユーザー こめだわら
提出日時 2026-04-18 11:56:09
言語 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  
実行時間 643 ms / 2,000 ms
コード長 2,649 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,501 ms
コンパイル使用メモリ 345,668 KB
実行使用メモリ 144,640 KB
最終ジャッジ日時 2026-04-18 11:56:45
合計ジャッジ時間 30,684 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void solve()':
main.cpp:87:19: warning: 'gi' may be used uninitialized [-Wmaybe-uninitialized]
   87 |     ll ans=dist[gi][gj];
      |                   ^
main.cpp:45:14: note: 'gi' was declared here
   45 |     ll si,sj,gi,gj;
      |              ^~
main.cpp:87:23: warning: 'gj' may be used uninitialized [-Wmaybe-uninitialized]
   87 |     ll ans=dist[gi][gj];
      |                       ^
main.cpp:45:17: note: 'gj' was declared here
   45 |     ll si,sj,gi,gj;
      |                 ^~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define rep(i,n) for(ll i=0;i<n;++i)
#define all(a) (a).begin(),(a).end()
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }
template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &a){
    if (a.empty()) return os;
    os << a.front();
    for (auto e : a | views::drop(1)){
        os << ' ' << e;
    }
    return os;
}

void dump(auto ...vs){
    ((cout << vs << ' '), ...) << endl;
}



void solve() {
    ll BH,BW;
    cin>>BH>>BW;
    ll H=2*BH-1;
    ll W=2*BW-1;
    vector<string> G(H,string(W,'.'));
    rep(i,BH){
        string S;
        cin>>S;
        rep(j,BW){
            G[2*i][2*j]=S[j];
        }
    }
    ll si,sj,gi,gj;
    rep(i,H){
        rep(j,W){
            if (G[i][j]=='S'){
                si=i;
                sj=j;
            }
            if (G[i][j]=='G'){
                gi=i;
                gj=j;
            }
        }
    }
    vector<ll> dxdy={0,1,0,-1,0};
    vector<vector<ll>> dist(H,vector<ll>(W,1e18));
    dist[si][sj]=0;
    queue<pair<ll,ll>> qu;
    qu.emplace(si,sj);
    while (qu.size()>0){
        auto [ci,cj]=qu.front();qu.pop();
        rep(d,4){
            ll di=dxdy[d];
            ll dj=dxdy[d+1];
            ll ni,nj;
            ni=ci+di;
            nj=cj+dj;
            if (0<=ni and ni<H and 0<=nj and nj<W and G[ni][nj]!='#'){
                if (chmin(dist[ni][nj],dist[ci][cj]+1)){
                    qu.emplace(ni,nj);
                }
            }
            ni=ci+2*di;
            nj=cj+2*dj;
            if ((di==0 or ci%2==0) and (dj==0 or cj%2==0)){
                if (0<=ni and ni<H and 0<=nj and nj<W and G[ni][nj]!='#'){
                    if (chmin(dist[ni][nj],dist[ci][cj]+1)){
                        qu.emplace(ni,nj);
                    }
                }
            }
        }
    }
    ll ans=dist[gi][gj];
    cout<<ans<<'\n';
    return;
}


int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll T=1;
    while (T--){
        solve();
    }
    return 0;
}
0