結果

問題 No.1323 うしらずSwap
ユーザー LuzhiledLuzhiled
提出日時 2020-12-07 23:07:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,216 bytes
コンパイル時間 3,338 ms
コンパイル使用メモリ 179,244 KB
実行使用メモリ 123,500 KB
最終ジャッジ日時 2023-10-21 09:32:43
合計ジャッジ時間 23,005 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
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 WA -
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 208 ms
95,780 KB
testcase_17 AC 530 ms
95,780 KB
testcase_18 AC 195 ms
96,044 KB
testcase_19 AC 561 ms
95,780 KB
testcase_20 AC 193 ms
95,780 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 194 ms
96,044 KB
testcase_27 AC 193 ms
96,044 KB
testcase_28 AC 406 ms
95,780 KB
testcase_29 AC 196 ms
96,044 KB
testcase_30 AC 409 ms
95,780 KB
testcase_31 AC 197 ms
95,780 KB
testcase_32 AC 515 ms
95,780 KB
testcase_33 AC 685 ms
108,716 KB
testcase_34 AC 755 ms
113,732 KB
testcase_35 AC 810 ms
116,372 KB
testcase_36 AC 780 ms
117,956 KB
testcase_37 AC 797 ms
118,484 KB
testcase_38 AC 984 ms
121,916 KB
testcase_39 AC 947 ms
122,708 KB
testcase_40 AC 1,002 ms
122,972 KB
testcase_41 AC 1,008 ms
123,500 KB
testcase_42 AC 860 ms
123,236 KB
testcase_43 AC 154 ms
79,412 KB
testcase_44 AC 332 ms
81,788 KB
testcase_45 AC 278 ms
86,804 KB
testcase_46 AC 326 ms
81,788 KB
testcase_47 AC 180 ms
82,052 KB
testcase_48 AC 153 ms
79,412 KB
testcase_49 AC 319 ms
81,788 KB
testcase_50 AC 142 ms
77,300 KB
testcase_51 AC 85 ms
68,588 KB
testcase_52 AC 109 ms
73,076 KB
testcase_53 AC 328 ms
82,052 KB
testcase_54 AC 107 ms
73,340 KB
testcase_55 AC 334 ms
82,052 KB
testcase_56 AC 92 ms
70,964 KB
testcase_57 AC 422 ms
86,804 KB
testcase_58 AC 3 ms
4,348 KB
testcase_59 AC 3 ms
4,348 KB
testcase_60 AC 2 ms
4,348 KB
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
  vector< int > rs(V);
  auto bfs = [&](int s) {
    queue< int > que;
    que.emplace(s);

    vector< int > res(V, inf);
    res[s] = 0;

    rs.assign(V, 0);
    rs[s] = 1;

    while (not que.empty()) {
      int v = que.front();
      que.pop();
      for (auto &u : G[v]) {
        if (res[u] == res[v] + 1) {
          rs[u] = 2;
        }
        if (res[u] > res[v] + 1) {
          res[u] = res[v] + 1;
          rs[u] = rs[v];
          que.emplace(u);
        }
      }
    }

    return res;
  };

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

  int ans = inf;
  if (rs[a] == 0) {
    ans = -1;
  }
  if (rs[a] >= 2) {
    ans = da[b] + db[a];
  }
  for (int y = 1; y < h - 1; y++) {
    for (int x = 1; x < w - 1; x++) {
      int v = to_v(y, x);
      if (rs[v] == 0) continue;
      if ((int)G[v].size() < 3) continue;

      int af = v;
      for (int i = 0; i < 4; i++) {
        int u = to_v(y + dy[i], x + dx[i]);
        if (da[u] > da[af]) continue;
        af = u;
      }
      
      int bf = v;
      for (int i = 0; i < 4; i++) {
        int u = to_v(y + dy[i], x + dx[i]);
        if (db[u] > db[bf]) continue;
        bf = u;
      }

      if (af == bf) {
        ans = min(ans, 2 * (da[v] + db[v]) + 4);
      } else {
        ans = min(ans, 2 * (da[v] + db[v]) + 2);
      }
    }
  }

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

  cout << ans << endl;
}
0