結果

問題 No.1323 うしらずSwap
ユーザー LuzhiledLuzhiled
提出日時 2020-12-16 01:41:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,467 ms / 3,000 ms
コード長 3,078 bytes
コンパイル時間 2,437 ms
コンパイル使用メモリ 215,464 KB
実行使用メモリ 234,072 KB
最終ジャッジ日時 2024-04-27 08:45:06
合計ジャッジ時間 29,552 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 319 ms
178,736 KB
testcase_17 AC 709 ms
178,648 KB
testcase_18 AC 324 ms
179,060 KB
testcase_19 AC 778 ms
178,484 KB
testcase_20 AC 323 ms
178,740 KB
testcase_21 AC 324 ms
178,876 KB
testcase_22 AC 753 ms
178,932 KB
testcase_23 AC 325 ms
178,680 KB
testcase_24 AC 769 ms
178,952 KB
testcase_25 AC 322 ms
178,736 KB
testcase_26 AC 291 ms
171,316 KB
testcase_27 AC 265 ms
162,388 KB
testcase_28 AC 491 ms
178,868 KB
testcase_29 AC 312 ms
179,116 KB
testcase_30 AC 487 ms
178,920 KB
testcase_31 AC 308 ms
178,772 KB
testcase_32 AC 588 ms
178,992 KB
testcase_33 AC 1,043 ms
204,592 KB
testcase_34 AC 1,111 ms
214,692 KB
testcase_35 AC 1,212 ms
219,836 KB
testcase_36 AC 1,149 ms
223,284 KB
testcase_37 AC 1,155 ms
224,544 KB
testcase_38 AC 1,385 ms
231,312 KB
testcase_39 AC 1,386 ms
232,616 KB
testcase_40 AC 1,454 ms
233,164 KB
testcase_41 AC 1,467 ms
234,072 KB
testcase_42 AC 1,273 ms
233,656 KB
testcase_43 AC 218 ms
145,960 KB
testcase_44 AC 380 ms
151,056 KB
testcase_45 AC 329 ms
161,028 KB
testcase_46 AC 376 ms
150,956 KB
testcase_47 AC 244 ms
151,252 KB
testcase_48 AC 234 ms
145,944 KB
testcase_49 AC 467 ms
151,044 KB
testcase_50 AC 220 ms
142,024 KB
testcase_51 AC 116 ms
124,556 KB
testcase_52 AC 162 ms
133,724 KB
testcase_53 AC 494 ms
151,244 KB
testcase_54 AC 159 ms
133,740 KB
testcase_55 AC 473 ms
151,224 KB
testcase_56 AC 133 ms
129,440 KB
testcase_57 AC 613 ms
160,788 KB
testcase_58 AC 3 ms
6,940 KB
testcase_59 AC 3 ms
6,940 KB
testcase_60 AC 3 ms
6,944 KB
testcase_61 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int inf = 1001001001;
constexpr int dy[] = {-1, 0, 1, 0};
constexpr int dx[] = {0, 1, 0, -1};
using Graph = vector< vector< int > >;

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

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

  auto from_v = [w](int v, int &y, int &x) {
    y = v / w;
    x = v % w;
  };

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

  Ya -= 1; Xa -= 1;
  Yb -= 1; Xb -= 1;

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

  vector< string > ss(h);
  for (auto &s : ss) cin >> s;
  ss[Ya][Xa] = 'A';
  ss[Yb][Xb] = 'B';

  int V = to_v(h, w);

  auto make_graph = [&](const vector< string > &ss) {
    Graph G(V);
    for (int y = 1; y + 1 < h; y++) {
      for (int x = 1; x + 1 < w; 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);
        }
      }
    }

    return G;
  };

  vector< int > pre(V, -1);

  auto bfs = [&](int s, const Graph &G) {
    int n = G.size();
    vector< pair< int, int > > d(n, {inf, 0});

    queue< int > que;
    
    que.push(s);
    d[s] = make_pair(0, 1);

    while (not que.empty()) {
      int v = que.front();
      que.pop();

      for (auto &u : G[v]) {
        if (d[u].first < d[v].first + 1) continue;
        if (d[u].first == d[v].first + 1) {
          d[u].second = 2;
          continue;
        }

        d[u] = d[v];
        d[u].first += 1;
        que.push(u);
        pre[u] = v;
      }
    }

    return d;
  };

  auto G = make_graph(ss);
  auto bs = bfs(b, G);
  auto as = bfs(a, G);

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

  if (as[b].second >= 2) {
    ans = 2 * as[b].first;
  }

  if (as[b].second != 0) {
    int v = pre[b];
    while (v != a) {
      int y, x;
      from_v(v, y, x);
      ss[y][x] = 'x';
      v = pre[v];
    }

    v = pre[b];
    while (v != a) {
      int y, x;
      from_v(v, y, x);
      for (int i = 0; i < 4; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (ss[ny][nx] == '.') {
          ans = min(ans, 2 * as[b].first + 2);
        }
      }
      v = pre[v];
    }
  }

  for (int v = 0; v < V; v++) {
    if (as[v].second == 0) continue;
    if ((int)G[v].size() < 3) continue;
    int cost = 2 * (as[v].first + bs[v].first) + 4;
    ans = min(ans, cost);
  }

  if (as[b].second != 0) {
    int v = pre[b];
    while (v != a) {
      int y, x;
      from_v(v, y, x);
      ss[y][x] = '#';
      v = pre[v];
    }

    ss[Yb][Xb] = '#';

    G = make_graph(ss);
    int cost = as[b].first + 1;
    as = bfs(a, G);

    for (int i = 0; i < 4; i++) {
      int y = Yb + dy[i];
      int x = Xb + dx[i];
      int v = to_v(y, x);
      if (v == a) continue;
      if (as[v].second == 0) continue;
      ans = min(ans, cost + as[v].first);
    }
  }

  if (ans == inf) ans = -1;
  cout << ans << endl;
}
0