結果

問題 No.3504 Insert Maze
コンテスト
ユーザー porkleoi
提出日時 2026-04-18 17:29:12
言語 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
結果
WA  
実行時間 -
コード長 4,482 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,568 ms
コンパイル使用メモリ 342,500 KB
実行使用メモリ 39,168 KB
最終ジャッジ日時 2026-04-18 17:29:39
合計ジャッジ時間 22,450 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 77 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a-1; i >= b; i--)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<vvvll> vvvvll;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<bool> vb;
typedef vector<vd> vvb;
typedef vector<vvd> vvvb;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef tuple<int, int, int> tui3;
typedef tuple<ll, ll, ll> tull3;
typedef priority_queue<int, vector<int>, greater<int>> pqi;
typedef priority_queue<vi, vector<vi>, greater<vi>> pqvi;
typedef priority_queue<pi, vector<pi>, greater<pi>> pqpi;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqll;
typedef priority_queue<vll, vector<vll>, greater<vll>> pqvll;
typedef priority_queue<pll, vector<pll>, greater<pll>> pqpll;
typedef priority_queue<pll, vector<pll>, less<pll>> rpqpll;
typedef priority_queue<int, vector<int>, less<int>> rpqi;
typedef priority_queue<vi, vector<vi>, less<vi>> rpqvi;
typedef priority_queue<tui3, vector<tui3>, greater<tui3>> pqtui3;
typedef priority_queue<tui3, vector<tui3>, less<tui3>> rpqtui3;
typedef priority_queue<tull3, vector<tull3>, greater<tull3>> pqtull3;
typedef priority_queue<tull3, vector<tull3>, less<tull3>> rpqtull3;
#define yes(ans) if(ans)cout << "yes"<< endl; else cout << "no" << endl
#define Yes(ans) if(ans)cout << "Yes"<< endl; else cout << "No" << endl
#define YES(ans) if(ans)cout << "YES"<< endl ;else cout << "NO" << endl
#define pos(ans) if(ans)cout << "Possible"<< endl ;else cout << "Impossible" << endl
#define printv(vec) {rep(i, 0, vec.size()) cout << vec[i] << ' '; cout << endl;}
#define printvv(vec) rep(i, 0, vec.size()) {rep(j, 0, vec[i].size()) cout << vec[i][j] << ' '; cout << endl;};
#define printvvv(vec) rep(i, 0, vec.size()) { rep(j, 0, vec[i].size()) { rep(k, 0, vec[i][j].size()) cout << vec[i][j][k] << ' '; cout << "	"; }cout << endl; };
#define all(x) x.begin(), x.end()
#define so(x) sort(all(x))
#define re(x) reverse(all(x))
#define rso(x) sort(x.rbegin(), x.rend())
#define vco(x, a) count(all(x), a)
#define per(x) next_permutation(all(x))
#define out(x) cout << x << endl
#define iINF 2147483647
#define llINF 9223372036854775807
#define INF 4000000000000000000
#define mod 998244353
#define mod2 1000000007
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}

int main() {
    int h, w; cin >> h >> w;
    vvc a(h, vc(w));
    rep(i, 0, h) rep(j, 0, w) cin >> a[i][j];
    a[0][0] = '.';
    a[h-1][w-1] = '.';
    vvi dp1(h, vi(w, mod));
    vvi dp2(h, vi(w, mod));
    dp1[0][0] = 0;
    rep(i, 0, h) rep(j, 0, w){
        if(i!=0){
            if(a[i][j]=='.' && a[i-1][j]=='.') dp1[i][j] = dp1[i-1][j]+1;
        }
        if(j!=0){
            if(a[i][j]=='.' && a[i][j-1]=='.') dp1[i][j] = dp1[i][j-1]+1;
        }
    }
    // printvv(dp1);
    dp2[h-1][w-1] = 0;
    rrep(i, h, 0) rrep(j, w, 0){
        if(i!=h-1){
            if(a[i][j]=='.' && a[i+1][j]=='.') dp2[i][j] = dp2[i+1][j]+1;
        }
        if(j!=w-1){
            if(a[i][j]=='.' && a[i][j+1]=='.') dp2[i][j] = dp2[i][j+1]+1;
        }
    }
    // printvv(dp2);
    if(dp1[h-1][w-1]<mod/2){
        cout << dp1[h-1][w-1] << endl;
        return 0;
    }
    vi yoko1(w, mod), tate1(h, mod);
    vi yoko2(w, 0), tate2(h, 0);
    rep(i, 0, h) rep(j, 0, w) if(dp1[i][j]<mod/2){
        chmin(yoko1[j], i);
        chmin(tate1[i], j);
    }
    rep(i, 0, h) rep(j, 0, w) if(dp2[i][j]<mod/2){
        chmax(yoko2[j], i);
        chmax(tate2[i], j);
    }
    bool p = false;
    rep(i, 0, h-1) if(tate1[i]<tate2[i+1]) p = true;
    rep(i, 0, w-1) if(yoko1[i]<yoko2[i+1]) p = true;
    if(p) cout << h+w-1 << endl;
    else cout << h+w << endl;
}
0