結果

問題 No.1323 うしらずSwap
ユーザー LuzhiledLuzhiled
提出日時 2020-12-16 01:29:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,605 ms / 3,000 ms
コード長 3,092 bytes
コンパイル時間 2,520 ms
コンパイル使用メモリ 214,768 KB
実行使用メモリ 234,036 KB
最終ジャッジ日時 2024-04-27 08:43:40
合計ジャッジ時間 32,885 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 342 ms
178,912 KB
testcase_17 AC 813 ms
178,644 KB
testcase_18 AC 339 ms
179,108 KB
testcase_19 AC 882 ms
178,660 KB
testcase_20 AC 338 ms
178,708 KB
testcase_21 AC 336 ms
178,880 KB
testcase_22 AC 788 ms
178,940 KB
testcase_23 AC 338 ms
178,732 KB
testcase_24 AC 816 ms
178,932 KB
testcase_25 AC 333 ms
178,680 KB
testcase_26 AC 302 ms
171,136 KB
testcase_27 AC 275 ms
162,388 KB
testcase_28 AC 520 ms
178,872 KB
testcase_29 AC 321 ms
179,112 KB
testcase_30 AC 523 ms
179,012 KB
testcase_31 AC 321 ms
178,660 KB
testcase_32 AC 622 ms
178,860 KB
testcase_33 AC 1,129 ms
204,684 KB
testcase_34 AC 1,195 ms
214,716 KB
testcase_35 AC 1,326 ms
219,824 KB
testcase_36 AC 1,234 ms
223,112 KB
testcase_37 AC 1,265 ms
224,492 KB
testcase_38 AC 1,517 ms
231,292 KB
testcase_39 AC 1,530 ms
232,532 KB
testcase_40 AC 1,605 ms
233,228 KB
testcase_41 AC 1,600 ms
234,036 KB
testcase_42 AC 1,363 ms
233,720 KB
testcase_43 AC 223 ms
145,864 KB
testcase_44 AC 404 ms
150,988 KB
testcase_45 AC 353 ms
160,860 KB
testcase_46 AC 394 ms
150,848 KB
testcase_47 AC 259 ms
151,220 KB
testcase_48 AC 241 ms
145,956 KB
testcase_49 AC 495 ms
150,888 KB
testcase_50 AC 227 ms
141,944 KB
testcase_51 AC 123 ms
124,528 KB
testcase_52 AC 171 ms
133,540 KB
testcase_53 AC 529 ms
151,336 KB
testcase_54 AC 162 ms
133,632 KB
testcase_55 AC 492 ms
151,192 KB
testcase_56 AC 134 ms
129,504 KB
testcase_57 AC 666 ms
160,860 KB
testcase_58 AC 4 ms
6,940 KB
testcase_59 AC 4 ms
6,940 KB
testcase_60 AC 3 ms
6,940 KB
testcase_61 AC 2 ms
6,940 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];
    }
  }

  for (int y = 1; y + 1 < h; y++) {
    for (int x = 1; x + 1 < w; x++) {
      if (ss[y][x] != 'x') continue;
      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);
        }
      }
    }
  }

  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);
  }

  {
    for (int y = 1; y + 1 < h; y++) {
      for (int x = 1; x + 1 < w; x++) {
        if (ss[y][x] != 'x') continue;
        ss[y][x] = '#';
      }
    }

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

    G = make_graph(ss);
    auto nas = 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 (nas[v].second == 0) continue;
      ans = min(ans, as[b].first + nas[v].first + 1);
    }
  }

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