結果

問題 No.3504 Insert Maze
コンテスト
ユーザー kq5y
提出日時 2026-04-18 00:38:36
言語 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  
実行時間 70 ms / 2,000 ms
コード長 2,407 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,635 ms
コンパイル使用メモリ 338,784 KB
実行使用メモリ 15,616 KB
最終ジャッジ日時 2026-04-18 00:40:48
合計ジャッジ時間 10,043 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

#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__)

#define sum(l) accumulate(l.begin(), l.end(), 0)
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)

using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vc = vector<char>;
using vvc = vector<vc>;

int main() {
  cin.tie(0); ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vs C(H);
  cin >> C;

  vvc from(H, vc(W, 0));
  vvc to(H, vc(W, 0));

  rep(i, H) {
    rep(j, W) {
      if (C[i][j] == '#') continue;
      if (i == 0 && j == 0) from[i][j] = 1;
      if (i > 0 && from[i - 1][j]) from[i][j] = 1;
      if (j > 0 && from[i][j - 1]) from[i][j] = 1;
    }
  }

  for (int i = H - 1; i >= 0; --i) {
    for (int j = W - 1; j >= 0; --j) {
      if (C[i][j] == '#') continue;
      if (i == H - 1 && j == W - 1) to[i][j] = 1;
      if (i + 1 < H && to[i + 1][j]) to[i][j] = 1;
      if (j + 1 < W && to[i][j + 1]) to[i][j] = 1;
    }
  }

  if (from[H - 1][W - 1]) {
    cout << H + W - 2 << '\n';
    return 0;
  }

  bool ok = false;

  rep(i, H - 1) {
    bool pref = false;
    rep(j, W) {
      if (from[i][j]) pref = true;
      if (pref && to[i + 1][j]) {
        ok = true;
        break;
      }
    }
    if (ok) break;
  }

  if (!ok) {
    rep(j, W - 1) {
      bool pref = false;
      rep(i, H) {
        if (from[i][j]) pref = true;
        if (pref && to[i][j + 1]) {
          ok = true;
          break;
        }
      }
      if (ok) break;
    }
  }

  if (ok) cout << H + W - 1 << '\n';
  else cout << H + W << '\n';

  return 0;
}
0