結果

問題 No.2213 Neq Move
ユーザー KudeKude
提出日時 2023-02-10 22:44:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 1,902 bytes
コンパイル時間 2,790 ms
コンパイル使用メモリ 238,500 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 23:53:07
合計ジャッジ時間 4,163 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 99 ms
4,376 KB
testcase_02 AC 85 ms
4,376 KB
testcase_03 AC 70 ms
4,380 KB
testcase_04 AC 60 ms
4,380 KB
testcase_05 AC 61 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while(tt--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c > d) swap(a, b), swap(c, d);
    map<P, ll> dp;
    priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> q;
    constexpr long long INF = 1002003004005006007;
    ll ans = INF;
    auto dist = [&](P x, P y) {
      return abs<ll>(x.first - y.first) + abs<ll>(x.second - y.second);
    };
    auto add = [&](P p, ll v) {
      auto [x, y] = p;
      if (x == y) return;
      if (x < y && x <= c && y <= d) {
        chmin(ans, v + dist(p, {c, d}));
      }
      if (dist(p, {a, b}) > 7 && dist(p, {a, 0}) > 7 && dist(p, {0, b}) > 7 && dist(p, {0, 0}) > 7) return;
      auto it = dp.find({x, y});
      if (it != dp.end() && it->second <= v) return;
      dp[{x, y}] = v;
      q.emplace(v, P(x, y));
    };
    add({a, b}, 0);
    while(q.size()) {
      auto [v, p] = q.top(); q.pop();
      if (dp[p] != v) continue;
      auto [x, y] = p;
      add({x + 1, y}, v + 1);
      add({x, y + 1}, v + 1);
      add({0, y}, v + 1);
      add({x, 0}, v + 1);
    }
    cout << ans << '\n';
  }
}
0