【題解】AtCoder ABC 184C – Super Ryuma

【題目敘述】https://atcoder.jp/contests/abc184/tasks/abc184_c

#include <bits/stdc++.h>
using namespace std;

long long a, b, c, d;

bool check(long long x, long long y){
    if (abs(a-x)+abs(b-y) <= 3) return true;
    return false;
}

int main(){
    cin >> a >> b >> c >> d;
    if (a == c && b == d) cout << 0 << "\n";
    else if (check(c, d) || (a+b == c+d) || (a-b == c-d)) cout << 1 << "\n";
    else if ((a+b)%2 == (c+d)%2) cout << 2 << "\n";
    else if (abs(a-c)+abs(b-d) <= 6 || check(c+d-b, b) || check(c-d+b, b)){
        cout << 2 << "\n";
    }
    else cout << 3 << "\n";
}

參考Editorial的寫法

#include <iostream>
using namespace std;
/*
 * Move-A: a+b = c+d
 * Move-B: a-b = c-d
 * Move-C: abs(a-c) + abs(b-d) <= 3
 */

int main(){
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int dx = c - a, dy = d - b;
    int ans = 0;
    if (dx == 0 && dy == 0){
        //(a,b)和(c,d)為同一點
        ans = 0;
    }else if (abs(dx) == abs(dy)){
        //Move-A or Move-B
        ans = 1;
    }else if (abs(dx) + abs(dy) <= 3){
        //Move-C
        ans = 1;
    }else if ((dx + dy) % 2 == 0){
        //Move-A + Move-B
        ans = 2;
    }else if (abs(dx) + abs(dy) <= 6){
        //Move-C + Move-C
        ans = 2;
    }else if (abs((a+b) - (c+d)) <= 3){
        //Move-A + Move-C
        ans = 2;
    }else if (abs((a-b) - (c-d)) <= 3){
        //Move-B + Move-C
        ans = 2;
    }else{
        ans = 3;
    }
    cout << ans << endl;
}
分享本文 Share with friends