結果

問題 No.2213 Neq Move
ユーザー milanis48663220milanis48663220
提出日時 2023-02-11 01:10:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,217 bytes
コンパイル時間 1,182 ms
コンパイル使用メモリ 125,960 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 01:35:16
合計ジャッジ時間 1,738 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using P = pair<int, int>;

int naive(int a, int b, int c, int d){
    auto dist = vec2d(21, 21, -1);
    queue<P> que;
    auto set_dist = [&](int i, int j, int d){
        if(i == j) return;
        if(i < 0 || i > 20 || j < 0 || j > 20) return;
        if(dist[i][j] == -1){
            que.push(P(i, j));
            dist[i][j] = d;
        }
    };
    set_dist(a, b, 0);
    while(!que.empty()){
        auto [i, j] = que.front(); que.pop();
        set_dist(i+1, j, dist[i][j]+1);
        set_dist(i, j+1, dist[i][j]+1);
        set_dist(i, 0, dist[i][j]+1);
        set_dist(0, j, dist[i][j]+1);
    }
    return dist[c][d];
}

bool ok(int a, int b, int c, int d){
    if(a > b){
        swap(a, b);
        swap(c, d);
    }
    if(c >= a && d >= b && d > c){
        return true;
    }
    return false;
}

ll solve(ll a, ll b, ll c, ll d){
    if(a > b){
        swap(a, b);
        swap(c, d);
    }
    if(c < a && d < b){
        return c+d+2;
    }

    if(c >= a && d >= b && d > c){
        return c-a+d-b;
    }

    if(ok(0, b, c, d)){
        return c+d-b+1;
    }

    if(ok(a, 0, c, d)){
        return c+d-a+1;
    }
    if(a == 1) return c+d+3;

    return c+d+2;
}

void test(){
    for(int a = 1; a <= 10; a++){
        for(int b = 1; b <= 10; b++){
            if(a == b) continue;
            for(int c = 1; c <= 10; c++){
                for(int d = 1; d <= 10; d++){
                    if(c == d) continue;
                    int ans0 = naive(a, b, c, d);
                    int ans1 = solve(a, b, c, d);
                    // if(ans1 == -1) continue;
                    if(ans0 == ans1) continue;
                    cout << a << ' ' << b << ' ' << c << ' ' << d << ':' << ans0 << ' ' << ans1 << endl;
                }
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    // test();
    int t; cin >> t;
    while(t--) {
        ll a, b, c, d; cin >> a >> b >> c >> d;
        cout << solve(a, b, c, d) << endl;
    }
}
0