結果

問題 No.2096 Rage With Our Friends
ユーザー KudeKude
提出日時 2022-10-07 23:16:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,931 bytes
コンパイル時間 2,666 ms
コンパイル使用メモリ 233,388 KB
実行使用メモリ 671,084 KB
最終ジャッジ日時 2023-09-03 15:32:34
合計ジャッジ時間 14,292 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,600 KB
testcase_01 AC 2 ms
5,396 KB
testcase_02 AC 2 ms
5,492 KB
testcase_03 AC 2 ms
5,416 KB
testcase_04 AC 2 ms
5,400 KB
testcase_05 AC 2 ms
5,396 KB
testcase_06 AC 2 ms
5,416 KB
testcase_07 AC 2 ms
5,416 KB
testcase_08 AC 2 ms
5,732 KB
testcase_09 AC 2 ms
5,576 KB
testcase_10 AC 2 ms
5,636 KB
testcase_11 AC 433 ms
359,308 KB
testcase_12 MLE -
testcase_13 AC 764 ms
436,960 KB
testcase_14 AC 1,201 ms
436,632 KB
testcase_15 AC 425 ms
359,300 KB
testcase_16 AC 2 ms
5,576 KB
testcase_17 AC 1,050 ms
436,732 KB
testcase_18 AC 2 ms
5,536 KB
testcase_19 AC 2 ms
5,516 KB
testcase_20 AC 2 ms
5,552 KB
testcase_21 AC 1,194 ms
443,208 KB
testcase_22 AC 1,210 ms
446,208 KB
testcase_23 AC 64 ms
34,524 KB
testcase_24 AC 758 ms
283,784 KB
testcase_25 AC 453 ms
166,228 KB
testcase_26 AC 271 ms
107,828 KB
testcase_27 AC 256 ms
101,524 KB
testcase_28 AC 6 ms
8,968 KB
testcase_29 AC 127 ms
53,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

constexpr int INF = 1001001001;
template <int k = 2, class T>  // cost in [0, k)
int kBFS(const vector<vector<pair<int, T>>>& g, int s, int goal) {
  static_assert(k >= 1);
  const int n = g.size();
  vector<int> dist(n, INF);
  vector<int> q[2 * k];
  dist[s] = 0;
  q[0].emplace_back(s);
  for (int dist_cur = 0, ptr = 0;;) {
    const auto qb = q + ptr;
    while (!qb[0].empty()) {
      int u = qb[0].back();
      qb[0].pop_back();
      if (dist[u] != dist_cur) continue;
      if (u == goal) return dist_cur;
      for (auto [v, c] : g[u]) {
        int nd = dist_cur + c;
        if (nd < dist[v]) {
          dist[v] = nd;
          qb[c].emplace_back(v);
        }
      }
    }
    do {
      dist_cur++;
      ptr++;
      if (ptr == 2 * k) return -1;
    } while (q[ptr].empty());
    if (ptr > k) {
      for (int i = ptr; i < 2 * k; i++) {
        swap(q[i], q[i - ptr]);
      }
      ptr = 0;
    }
  }
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  char s[2010][2010];
  rep(i, h) cin >> s[i];
  vector<vector<pair<int, bool>>> to(h * w * 2);
  const int tot = h * w;
  rep(i, h - 1) rep(j, w) {
    int v = tot + i * w + j;
    to[v + w].emplace_back(v, 0);
  }
  rep(i, h) rep(j, w) if (s[i][j] == '.') {
    int v = i * w + j;
    to[tot + v].emplace_back(v, 0);
  }
  short i_last[2000][2000];
  rep(j, w) {
    int il = 0;
    rep(i, h) {
      if (s[i][j] == '.') il = i;
      i_last[i][j] = il;
    }
  }
  rep(i, h) rep(j, w) if (s[i][j] == '.') {
    for(int dj: {1, -1}) {
      int nj = j + dj;
      if (!(0 <= nj && nj < w)) continue;
      to[i*w + j].emplace_back(tot + min(h-1, i+1)*w + nj, 0);
      int il = i_last[min(h-1, i+1)][nj];
      nj += dj;
      if (0 <= nj && nj < w) {
        int e = max(0, i - il);
        to[i*w + j].emplace_back(tot + min(h-1, il + 1 + e / 2)*w + nj, 0);
        to[i*w + j].emplace_back(tot + min(h-1, i+1)*w + nj, 1);
      }
    }
    if (w != 1) {
      to[i*w + j].emplace_back(tot + min(h-1, i+1)*w + j, 1);
    }
  }
  int ans = kBFS(to, sx*w + sy, gx * w + gy);
  cout << ans << '\n';
}
0