結果

問題 No.3504 Insert Maze
コンテスト
ユーザー dayo ZOI
提出日時 2026-04-19 00:34:04
言語 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
結果
RE  
実行時間 -
コード長 2,323 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,775 ms
コンパイル使用メモリ 193,076 KB
実行使用メモリ 449,292 KB
最終ジャッジ日時 2026-04-19 00:34:48
合計ジャッジ時間 26,366 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 WA * 14 RE * 52
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <string>
#include <iostream>
#include <vector>
#include <stack>
#include <cassert>
#include <queue>

using namespace std;
using ll = long long;

#define rep(i, n) for (int i = 0; i < n; ++i)

int main()
{
    ll h, w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i, h) cin >> s[i];
    s[0][0] = '.';
    s[h-1][w-1] = '.';

    auto itop = [&](ll i) {
        return pair{i / w, i % w};
    };
    auto ptoi = [&](pair<ll, ll> p) {
        return p.first * w + p.second;
    };

    vector<vector<ll>> g1(h * w), g2(h * w);
    rep(i, h) {
        rep(j, w) {
            if(s[i][j] == '.') {
                if(i < h-1 && s[i+1][j] == '.') g1[ptoi({i, j})].emplace_back(ptoi({i+1, j}));
                if(j < w-1 && s[i][j+1] == '.') g1[ptoi({i, j})].emplace_back(ptoi({i, j+1}));
            }
        }
    }
    rep(i, h) {
        rep(j, w) {
            if(s[i][j] == '.') {
                if(i > 0 && s[i-1][j] == '.') g2[ptoi({i, j})].emplace_back(ptoi({i-1, j}));
                if(j > 0 && s[i][j-1] == '.') g2[ptoi({i, j})].emplace_back(ptoi({i, j-1}));
            }
        }
    }

    vector<bool> dist1(h*w, false), dist2(h * w, false);
    dist1[0] = true;
    dist2.back() = true;
    queue<ll> q1, q2;
    q1.push(0);
    q2.push((h-1) * (w-1));

    while(!q1.empty()) {
        auto v = q1.front();
        q1.pop();
        for(auto c : g1[v]) {
            if(dist1[c]) continue;
            dist1[c] = true;
            q1.emplace(c);
        }
    }
    while(!q2.empty()) {
        auto v = q2.front();
        q2.pop();
        for(auto c : g2[v]) {
            if(dist2[c]) continue;
            dist2[c] = true;
            q2.emplace(c);
        }
    }

    if(dist1.back()) {
        cout << h + w - 2 << endl;
        return 0;
    }
    rep(i, h-1) {
        ll j = 0;
        while(j < w && !dist1[i * w + j]) j++;
        for(; j<w; j++) {
            if(dist2[(i+1) * w + j]) {
                cout << h + w - 1 << endl;
                return 0;
            }
        }
    }
    rep(j, w-1) {
        ll i = 0;
        while(i < h && !dist1[i * w + j]) i++;
        for(; i<h; i++) {
            if(dist2[i * w + (j+1)]) {
                cout << h + w - 1 << endl;
                return 0;
            }
        }
    }
    cout << h + w << endl;
    return 1;
}
0