题解:AT_abc184_c [ABC184C] Super Ryuma

· · 题解

AT_abc184_c [ABC184C] Super Ryuma题解

题目大意

有一个无限大的棋盘,问从 (r_1,c_1) 到 (r_2,c_2) 最少需要多少步。从 (a,b) 能一步走到 (c,d) 需满足如下条件之一:

思路分析

显然答案不会超过 3,那么就可以分别讨论。

0 步

起点终点相同 r_1=r_2,c_1=c_2。

1 步

满足三种条件之一。

2 步

AC Code

#include<bits/stdc++.h>
#define int long long//1e9要开long long
using namespace std;
typedef pair<int, int>PII;
const int N = 3e5+10, mod = 998244353;
int r1, c1, r2, c2;
void solve() {
    cin >> r1 >> c1 >> r2 >> c2;
    int t1 = r1 - c1;
    int t2 = r2 - c2;

    int s1 = r1 + c1;
    int s2 = r2 + c2;

    int dx = abs(r1 - r2);
    int dy = abs(c1 - c2);
    int k = dx + dy;

    int ans = 0;
    if (r1 == r2 && c1 == c2) {
        ans = 0;
    } else if (s1 == s2 || t1 == t2 || k <= 3) {
        ans = 1;
    } else if (k <= 6 || abs(s1 - s2) <= 3 || abs(t1 - t2) <= 3 || (s1 + s2) % 2 == 0 || (t1 + s2) % 2 == 0) {
        ans = 2;
    } else {
        ans = 3;
    }
    cout << ans << endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
//  cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}