結果

問題 No.1323 うしらずSwap
ユーザー 👑 emthrmemthrm
提出日時 2020-12-20 02:03:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 340 ms / 3,000 ms
コード長 4,142 bytes
コンパイル時間 2,719 ms
コンパイル使用メモリ 224,052 KB
実行使用メモリ 53,888 KB
最終ジャッジ日時 2023-08-09 13:52:18
合計ジャッジ時間 12,135 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 100 ms
53,708 KB
testcase_17 AC 180 ms
53,836 KB
testcase_18 AC 101 ms
53,708 KB
testcase_19 AC 193 ms
53,772 KB
testcase_20 AC 102 ms
53,836 KB
testcase_21 AC 101 ms
53,784 KB
testcase_22 AC 180 ms
53,788 KB
testcase_23 AC 100 ms
53,632 KB
testcase_24 AC 181 ms
53,636 KB
testcase_25 AC 102 ms
53,636 KB
testcase_26 AC 107 ms
53,724 KB
testcase_27 AC 112 ms
53,652 KB
testcase_28 AC 150 ms
53,716 KB
testcase_29 AC 111 ms
53,764 KB
testcase_30 AC 199 ms
53,716 KB
testcase_31 AC 110 ms
53,888 KB
testcase_32 AC 205 ms
53,708 KB
testcase_33 AC 340 ms
53,184 KB
testcase_34 AC 311 ms
53,516 KB
testcase_35 AC 315 ms
53,516 KB
testcase_36 AC 298 ms
53,820 KB
testcase_37 AC 287 ms
53,464 KB
testcase_38 AC 278 ms
53,156 KB
testcase_39 AC 279 ms
53,372 KB
testcase_40 AC 280 ms
53,384 KB
testcase_41 AC 284 ms
53,784 KB
testcase_42 AC 260 ms
53,372 KB
testcase_43 AC 85 ms
53,652 KB
testcase_44 AC 124 ms
53,560 KB
testcase_45 AC 124 ms
53,712 KB
testcase_46 AC 117 ms
53,560 KB
testcase_47 AC 99 ms
53,652 KB
testcase_48 AC 87 ms
53,652 KB
testcase_49 AC 138 ms
53,620 KB
testcase_50 AC 81 ms
53,448 KB
testcase_51 AC 48 ms
53,652 KB
testcase_52 AC 62 ms
53,504 KB
testcase_53 AC 127 ms
53,728 KB
testcase_54 AC 64 ms
53,452 KB
testcase_55 AC 141 ms
53,496 KB
testcase_56 AC 56 ms
53,876 KB
testcase_57 AC 147 ms
53,708 KB
testcase_58 AC 2 ms
4,384 KB
testcase_59 AC 3 ms
4,380 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int h, w, ra, ca, rb, cb; cin >> h >> w >> ra >> ca >> rb >> cb; --ra; --ca; --rb; --cb;
  vector<string> s(h); REP(i, h) cin >> s[i];
  vector dist(h, vector(w, -1));
  dist[ra][ca] = 0;
  vector prev(h, vector(w, make_pair(-1, -1)));
  queue<pair<int, int>> que({{ra, ca}});
  while (!que.empty()) {
    auto [i, j] = que.front(); que.pop();
    REP(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (dist[y][x] == -1 && s[y][x] == '.') {
        dist[y][x] = dist[i][j] + 1;
        prev[y][x] = {i, j};
        que.emplace(y, x);
      }
    }
  }
  if (dist[rb][cb] == -1) {
    cout << "-1\n";
    return 0;
  }
  int ans = INF;
  // {
  //   int cnt = 0;
  //   REP(k, 4) {
  //     int y = ra + dy[k], x = ca + dx[k];
  //     cnt += s[y][x] == '.';
  //   }
  //   if (cnt >= 3) chmin(ans, dist[rb][cb] * 2 + 4);
  // }
  // {
  //   int cnt = 0;
  //   REP(k, 4) {
  //     int y = rb + dy[k], x = cb + dx[k];
  //     cnt += s[y][x] == '.';
  //   }
  //   if (cnt >= 3) chmin(ans, dist[rb][cb] * 2 + 4);
  // }
  s[rb][cb] = 'b';
  int cr = rb, cc = cb;
  while (cr != ra || cc != ca) {
    tie(cr, cc) = prev[cr][cc];
    s[cr][cc] = '$';
  }
  s[ra][ca] = 'a';
  REP(i, h) REP(j, w) {
    if (s[i][j] == '$') {
      REP(k, 4) {
        int y = i + dy[k], x = j + dx[k];
        if (s[y][x] == '.') chmin(ans, dist[rb][cb] * 2 + 2);
      }
    }
  }
  s[ra][ca] = s[rb][cb] = '$';
  // REP(i, h) cout << s[i] << '\n';

  // 迂回路を探索しているが嘘解法?
  vector bfs(h, vector(w, make_pair(-1, -1)));
  queue<tuple<int, int, int>> que2;
  REP(i, h) REP(j, w) {
    if (s[i][j] == '$') {
      bfs[i][j] = {dist[i][j], 0};
      que2.emplace(i, j, dist[i][j]);
    }
  }
  while (!que2.empty()) {
    auto [i, j, d] = que2.front(); que2.pop();
    REP(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (s[y][x] == '.') {
        if (bfs[y][x].first == -1) {
          bfs[y][x] = {d, bfs[i][j].second + 1};
          que2.emplace(y, x, d);
        } else if (bfs[y][x].first != bfs[i][j].first) {
          int tmp = bfs[y][x].second + bfs[i][j].second + 1 - abs(bfs[y][x].first - bfs[i][j].first);
          assert(tmp >= 0);
          chmin(ans, dist[rb][cb] * 2 + tmp);
        }
      }
    }
  }

  vector dista(h, vector(w, -1));
  dista[ra][ca] = 0;
  que.emplace(ra, ca);
  while (!que.empty()) {
    auto [i, j] = que.front(); que.pop();
    int cnt = 0;
    REP(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (s[y][x] != '#') {
        ++cnt;
        if (dista[y][x] == -1) {
          dista[y][x] = dista[i][j] + 1;
          que.emplace(y, x);
        }
      }
    }
    if (cnt >= 3) chmin(ans, dist[rb][cb] * 2 + (dista[i][j] + 1) * 4);
  }
  vector distb(h, vector(w, -1));
  distb[rb][cb] = 0;
  que.emplace(rb, cb);
  while (!que.empty()) {
    auto [i, j] = que.front(); que.pop();
    int cnt = 0;
    REP(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (s[y][x] != '#') {
        ++cnt;
        if (distb[y][x] == -1) {
          distb[y][x] = distb[i][j] + 1;
          que.emplace(y, x);
        }
      }
    }
    if (cnt >= 3) chmin(ans, dist[rb][cb] * 2 + (distb[i][j] + 1) * 4);
  }
  cout << (ans == INF ? -1 : ans) << '\n';
  return 0;
}
0