結果

問題 No.3504 Insert Maze
コンテスト
ユーザー aorinngo0606
提出日時 2026-04-19 14:01:51
言語 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  
実行時間 263 ms / 2,000 ms
コード長 2,958 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,984 ms
コンパイル使用メモリ 378,240 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2026-04-19 15:21:23
合計ジャッジ時間 22,349 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
typedef long long ll;
typedef long double lb;
using namespace std;
using namespace atcoder;
#define all(x) (x).begin(), (x).end()
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
template<class T> bool chmin(T& a, const T& b){if(a > b){a = b; return 1;} return 0;}
template<class T> bool chmax(T& a, const T& b){if(a < b){a = b; return 1;} return 0;}
const ll INF = 1000000000000000000; // 10^18

int main(){
    ll h,w;
    cin >> h >> w;
    vector<vector<char>> c(h,vector<char>(w));
    rep(i,h){
        rep(j,w){
            cin >> c[i][j];
        }
    }
    //h = iにSがあるか
    vector<bool> S_h(h,false);
    vector<bool> S_w(w,false);
    vector<bool> G_h(h,false);
    vector<bool> G_w(w,false);
    S_h[0] = true;
    S_w[0] = true;
    G_h[h-1] = true;
    G_w[w-1] = true;
    //Sを埋める
    rep(i,h){
        rep(j,w){
            if(c[i][j] == 'G'){
                if(i != 0 && c[i-1][j] == 'S'){
                    c[i][j] = 'S';
                    S_h[i] = true;
                    S_w[j] = true;
                    cout << h+w-2 << endl;
                    return 0;
                }
                if(j != 0 && c[i][j-1] == 'S'){
                    c[i][j] = 'S';
                    S_h[i] = true;
                    S_w[j] = true;
                    cout << h+w-2 << endl;
                    return 0;
                }
            }
            if(c[i][j] == '.'){
                if(i != 0 && c[i-1][j] == 'S'){
                    c[i][j] = 'S';
                    S_h[i] = true;
                    S_w[j] = true;
                }
                if(j != 0 && c[i][j-1] == 'S'){
                    c[i][j] = 'S';
                    S_h[i] = true;
                    S_w[j] = true;
                }
            }
        }
    }
    //Gを埋める
    for(ll i = h-1; i >= 0;i--){
        for(ll j = w-1;j >= 0;j--){
            if(c[i][j] == '.'){
                if(i != h-1 && c[i+1][j] == 'G'){
                    c[i][j] = 'G';
                    G_h[i] = true;
                    G_w[j] = true;
                }
                if(j != w-1 && c[i][j+1] == 'G'){
                    c[i][j] = 'G';
                    G_h[i] = true;
                    G_w[j] = true;
                }
            }
        }
    }
    //一つ挿入で行けるか
    //hについて、(0-indexの)iとi+1の間に挿入できるか
    for(int i = 0;i < h-1;i++){
        if(S_h[i] && G_h[i+1]){
            cout << h+w-1 << endl;
            return 0;
        }
    }
    for(int j = 0;j < w-1;j++){
        if(S_w[j] && G_w[j+1]){
            cout << h+w-1 << endl;
            return 0;
        }
    }
    cout << h + w << endl;
    return 0;
}
0