結果

問題 No.1323 うしらずSwap
ユーザー 👑 emthrmemthrm
提出日時 2020-12-20 01:44:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,403 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 219,380 KB
実行使用メモリ 26,260 KB
最終ジャッジ日時 2023-10-21 10:09:36
合計ジャッジ時間 7,405 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int h, w, ra, ca, rb, cb; cin >> h >> w >> ra >> ca >> rb >> cb; --ra; --ca; --rb; --cb;
  vector<string> s(h); REP(i, h) cin >> s[i];
  vector dist(h, vector(w, -1));
  dist[ra][ca] = 0;
  vector prev(h, vector(w, make_pair(-1, -1)));
  queue<pair<int, int>> que({{ra, ca}});
  while (!que.empty()) {
    auto [i, j] = que.front(); que.pop();
    REP(k, 4) {
      int y = i + dy[k], x = j + dx[k];
      if (dist[y][x] == -1 && s[y][x] == '.') {
        dist[y][x] = dist[i][j] + 1;
        prev[y][x] = {i, j};
        que.emplace(y, x);
      }
    }
  }
  if (dist[rb][cb] == -1) {
    cout << "-1\n";
    return 0;
  }
  int ans = INF;
  {
    int cnt = 0;
    REP(k, 4) {
      int y = ra + dy[k], x = ca + dx[k];
      cnt += s[y][x] == '.';
    }
    if (cnt >= 3) chmin(ans, dist[rb][cb] * 2 + 4);
  }
  {
    int cnt = 0;
    REP(k, 4) {
      int y = rb + dy[k], x = cb + dx[k];
      cnt += s[y][x] == '.';
    }
    if (cnt >= 3) chmin(ans, dist[rb][cb] * 2 + 4);
  }
  s[rb][cb] = 'b';
  int cr = rb, cc = cb;
  while (cr != ra || cc != ca) {
    tie(cr, cc) = prev[cr][cc];
    s[cr][cc] = '$';
  }
  s[ra][ca] = 'a';
  REP(i, h) REP(j, w) {
    if (s[i][j] == '$') {
      REP(k, 4) {
        int y = i + dy[k], x = j + dx[k];
        if (s[y][x] == '.') chmin(ans, dist[rb][cb] * 2 + 2);
      }
    }
  }
  s[ra][ca] = s[rb][cb] = '$';
  // REP(i, h) cout << s[i] << '\n';
  // cout << (ans == INF ? -1 : ans) << '\n';
  cout << dist[rb][cb] * 2 + 1 << '\n';
  return 0;
}
0