結果

問題 No.1323 うしらずSwap
ユーザー LuzhiledLuzhiled
提出日時 2020-12-10 02:05:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,460 bytes
コンパイル時間 2,402 ms
コンパイル使用メモリ 179,760 KB
実行使用メモリ 296,060 KB
最終ジャッジ日時 2023-10-21 09:36:21
合計ジャッジ時間 42,089 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 449 ms
213,476 KB
testcase_17 AC 1,260 ms
213,212 KB
testcase_18 AC 450 ms
213,672 KB
testcase_19 AC 1,380 ms
213,188 KB
testcase_20 AC 444 ms
213,388 KB
testcase_21 AC 456 ms
213,456 KB
testcase_22 AC 1,530 ms
213,456 KB
testcase_23 AC 447 ms
213,192 KB
testcase_24 AC 1,496 ms
213,456 KB
testcase_25 AC 450 ms
213,192 KB
testcase_26 AC 367 ms
213,720 KB
testcase_27 AC 362 ms
213,720 KB
testcase_28 AC 792 ms
213,456 KB
testcase_29 AC 354 ms
213,720 KB
testcase_30 AC 865 ms
213,456 KB
testcase_31 AC 352 ms
213,192 KB
testcase_32 AC 926 ms
213,456 KB
testcase_33 AC 1,545 ms
252,168 KB
testcase_34 AC 1,686 ms
267,196 KB
testcase_35 AC 1,751 ms
274,976 KB
testcase_36 AC 1,696 ms
279,740 KB
testcase_37 AC 1,758 ms
281,804 KB
testcase_38 AC 1,999 ms
292,364 KB
testcase_39 AC 1,987 ms
294,064 KB
testcase_40 AC 2,080 ms
295,004 KB
testcase_41 AC 2,099 ms
296,060 KB
testcase_42 AC 1,798 ms
295,532 KB
testcase_43 AC 359 ms
163,844 KB
testcase_44 AC 543 ms
171,648 KB
testcase_45 AC 542 ms
186,500 KB
testcase_46 AC 533 ms
171,648 KB
testcase_47 AC 437 ms
171,980 KB
testcase_48 AC 354 ms
163,844 KB
testcase_49 AC 563 ms
171,716 KB
testcase_50 AC 312 ms
158,036 KB
testcase_51 AC 115 ms
131,852 KB
testcase_52 AC 201 ms
145,580 KB
testcase_53 AC 537 ms
172,028 KB
testcase_54 AC 201 ms
145,628 KB
testcase_55 WA -
testcase_56 AC 157 ms
139,028 KB
testcase_57 AC 712 ms
186,284 KB
testcase_58 AC 4 ms
4,348 KB
testcase_59 AC 3 ms
4,348 KB
testcase_60 AC 4 ms
4,348 KB
testcase_61 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In lambda function:
main.cpp:55:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   55 |       auto [v, pre, cost] = que.front();
      |            ^

ソースコード

diff #

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

int main() {
  int h, w;
  cin >> h >> w;

  auto to_v = [w](int y, int x) {
    return y * w + x;
  };

  int Ya, Xa, Yb, Xb;
  cin >> Ya >> Xa >> Yb >> Xb;

  int a = to_v(Ya - 1, Xa - 1);
  int b = to_v(Yb - 1, Xb - 1);

  vector< string > ss(h);
  for (auto &s : ss) cin >> s;

  int V = h * w;
  vector< vector< int > > G(V);

  constexpr int dy[] = {-1, 0, 1, 0};
  constexpr int dx[] = {0, 1, 0, -1};
  for (int y = 1; y < h - 1; y++) {
    for (int x = 1; x < w - 1; x++) {
      if (ss[y][x] == '#') continue;

      int v = to_v(y, x);
      for (int i = 0; i < 4; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (ss[ny][nx] == '#') continue;
        int u = to_v(ny, nx);
        G[v].emplace_back(u);
      }
    }
  }

  constexpr int inf = 1001001001;
  auto bfs = [&](int s) {
    vector< vector< int > > res(V);
    res[s].emplace_back(0);

    struct t {
      int v, pre, cost;
      t(int v_, int pre_, int cost_) : v(v_), pre(pre_), cost(cost_) {}
    };

    queue< t > que;
    que.emplace(s, -1, 0);

    while (not que.empty()) {
      auto [v, pre, cost] = que.front();
      que.pop();

      for (auto &u : G[v]) {
        if (u == pre) continue;
        if ((int)res[u].size() == 2) continue;
        res[u].emplace_back(cost + 1);
        que.emplace(u, v, cost + 1);
      }
    }

    return res;
  };

  auto da = bfs(a);
  auto db = bfs(b);

  int ans = inf;
  if ((int)da[b].size() == 0) {
    ans = -1;
  }

  if ((int)da[b].size() == 2) {
    ans = min(ans, da[b][0] + db[a][1]);
  }

  auto find_fromv = [&](int y, int x, const vector< vector< int > > &dist) {
    int f = to_v(y, x);
    for (int i = 0; i < 4; i++) {
      int u = to_v(y + dy[i], x + dx[i]);
      if ((int)dist[u].size() == 0) continue;
      if (dist[u][0] > dist[f][0]) continue;
      f = u;
    }
    return f;
  };

  for (int y = 1; y < h - 1; y++) {
    for (int x = 1; x < w - 1; x++) {
      int v = to_v(y, x);
      if ((int)da[v].size() == 0) continue;
      if ((int)db[v].size() == 0) continue;
      if ((int)G[v].size() < 3) continue;

      int af = find_fromv(y, x, da);
      int bf = find_fromv(y, x, db);

      int cost = 2 * (da[v][0] + db[v][0]);
      if (af == bf or af == v or bf == v) {
        cost += 4;
      } else {
        cost += 2;
      }

      ans = min(ans, cost);
    }
  }

  if (ans == inf) {
    ans = -1;
  }

  cout << ans << endl;
}
0