結果
問題 |
No.2213 Neq Move
|
ユーザー |
![]() |
提出日時 | 2023-02-10 22:44:02 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 99 ms / 2,000 ms |
コード長 | 1,902 bytes |
コンパイル時間 | 2,704 ms |
コンパイル使用メモリ | 231,096 KB |
最終ジャッジ日時 | 2025-02-10 13:09:24 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 5 |
ソースコード
#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'; } }