結果

問題 No.1323 うしらずSwap
ユーザー ei1333333ei1333333
提出日時 2020-12-07 23:18:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,069 bytes
コンパイル時間 2,470 ms
コンパイル使用メモリ 215,352 KB
実行使用メモリ 35,612 KB
最終ジャッジ日時 2023-10-21 09:34:26
合計ジャッジ時間 14,854 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 RE -
testcase_02 AC 1 ms
4,348 KB
testcase_03 RE -
testcase_04 AC 2 ms
4,348 KB
testcase_05 RE -
testcase_06 AC 2 ms
4,348 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 3 ms
4,348 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 76 ms
27,240 KB
testcase_17 AC 117 ms
27,184 KB
testcase_18 AC 78 ms
27,216 KB
testcase_19 AC 123 ms
27,224 KB
testcase_20 AC 76 ms
27,188 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 103 ms
27,180 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 167 ms
26,972 KB
testcase_34 AC 178 ms
27,052 KB
testcase_35 AC 175 ms
27,084 KB
testcase_36 AC 168 ms
27,176 KB
testcase_37 AC 166 ms
27,096 KB
testcase_38 AC 175 ms
26,976 KB
testcase_39 AC 169 ms
27,056 KB
testcase_40 AC 182 ms
27,084 KB
testcase_41 AC 174 ms
27,180 KB
testcase_42 AC 163 ms
27,096 KB
testcase_43 AC 70 ms
27,160 KB
testcase_44 AC 97 ms
27,096 KB
testcase_45 AC 90 ms
27,240 KB
testcase_46 AC 101 ms
27,096 KB
testcase_47 AC 78 ms
27,160 KB
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 RE -
testcase_61 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using pi = pair< int, int >;
constexpr int inf = 1 << 30;
constexpr int vy[] = {-1, 1, 0, 0};
constexpr int vx[] = {0, 0, -1, 1};

template< typename T, typename E >
bool chmin(T &a, E b) {
  if(a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

int main() {
  int H, W, Ya, Xa, Yb, Xb;
  cin >> H >> W >> Ya >> Xa >> Yb >> Xb;
  --Ya, --Xa, --Yb, --Xb;
  vector< string > S(H);
  for(auto &s : S) cin >> s;

  vector< vector< int > > min_cost(H, vector< int >(W, inf));
  vector< vector< int > > ways(H, vector< int >(W, 0));
  vector< vector< int > > from(H, vector< int >(W, -1));

  {
    queue< pi > que;
    min_cost[Ya][Xa] = 0;
    ways[Ya][Xa] = 1;
    que.emplace(Ya, Xa);
    while(!que.empty()) {
      auto[y, x] = que.front();
      que.pop();
      for(int k = 0; k < 4; k++) {
        int ny = y + vy[k];
        int nx = x + vx[k];
        if(S[ny][nx] == '#') continue;
        if(chmin(min_cost[ny][nx], min_cost[y][x] + 1)) {
          from[ny][nx] = k;
          ways[ny][nx] = 0;
          que.emplace(ny, nx);
        }
        if(min_cost[y][x] + 1 == min_cost[ny][nx]) {
          ways[ny][nx] += ways[y][x];
          chmin(ways[ny][nx], 2);
        }
      }
    }
  }

  if(ways[Yb][Xb] == 0) {
    cout << -1 << "\n";
    return 0;
  }

  if(ways[Yb][Xb] >= 2) {
    cout << min_cost[Yb][Xb] * 2 << "\n";
    return 0;
  }

  vector< pair< int, int > > path;
  int cy = Yb, cx = Xb;
  path.emplace_back(cy, cx);
  while(cy != Ya || cx != Xa) {
    int py = cy + vy[from[cy][cx] ^ 1];
    int px = cx + vx[from[cy][cx] ^ 1];
    cy = py;
    cx = px;
    path.emplace_back(cy, cx);
  }

  assert(min_cost[Yb][Xb] + 1 == path.size());

  for(auto &p : path) S[p.first][p.second] = '*';

  for(int i = 1; i + 1 < path.size(); i++) {
    for(int j = 0; j < 4; j++) {
      int ny = path[i].first + vy[j];
      int nx = path[i].second + vx[j];
      if(S[ny][nx] == '.') {
        cout << min_cost[Yb][Xb] * 2 + 2 << "\n";
        return 0;
      }
    }
  }

  assert(0);
}
0