結果

問題 No.2096 Rage With Our Friends
ユーザー KudeKude
提出日時 2022-10-07 23:20:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,157 ms / 5,000 ms
コード長 2,835 bytes
コンパイル時間 2,480 ms
コンパイル使用メモリ 231,892 KB
実行使用メモリ 444,776 KB
最終ジャッジ日時 2023-09-03 15:42:02
合計ジャッジ時間 8,819 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,424 KB
testcase_01 AC 2 ms
5,564 KB
testcase_02 AC 2 ms
5,488 KB
testcase_03 AC 2 ms
5,484 KB
testcase_04 AC 2 ms
5,464 KB
testcase_05 AC 2 ms
5,448 KB
testcase_06 AC 2 ms
5,540 KB
testcase_07 AC 2 ms
5,556 KB
testcase_08 AC 2 ms
5,572 KB
testcase_09 AC 3 ms
7,584 KB
testcase_10 AC 2 ms
5,464 KB
testcase_11 AC 110 ms
132,600 KB
testcase_12 AC 1,157 ms
444,776 KB
testcase_13 AC 383 ms
210,548 KB
testcase_14 AC 565 ms
210,432 KB
testcase_15 AC 119 ms
132,532 KB
testcase_16 AC 2 ms
5,772 KB
testcase_17 AC 388 ms
210,352 KB
testcase_18 AC 2 ms
5,508 KB
testcase_19 AC 2 ms
5,552 KB
testcase_20 AC 2 ms
5,568 KB
testcase_21 AC 549 ms
216,492 KB
testcase_22 AC 564 ms
219,672 KB
testcase_23 AC 31 ms
28,036 KB
testcase_24 AC 346 ms
140,532 KB
testcase_25 AC 239 ms
95,780 KB
testcase_26 AC 140 ms
66,524 KB
testcase_27 AC 112 ms
52,640 KB
testcase_28 AC 5 ms
10,376 KB
testcase_29 AC 54 ms
29,372 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);
  int i_last[2000][2000];
  rep(j, w) {
    int il = 0;
    rep(i, h) {
      if (s[i][j] == '.') il = i;
      i_last[i][j] = il;
    }
  }
  for(int i = 1; i < h; i++) rep(j, w) if (s[i][j] == '.') {
    to[i*w + j].emplace_back(i_last[i-1][j], 0);
  }
  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(i_last[min(h-1, i+1)][nj]*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(i_last[min(h-1, il + 1 + e / 2)][nj]*w + nj, 0);
        to[i*w + j].emplace_back(i_last[min(h-1, i+1)][nj]*w + nj, 1);
      }
    }
    if (w != 1) {
      to[i*w + j].emplace_back(i_last[min(h-1, i+1)][j]*w + j, 1);
    }
  }
  int ans = kBFS(to, sx*w + sy, gx * w + gy);
  cout << ans << '\n';
}
0