跳到正文

题解

题解:AT_abc184_c [ABC184C] Super Ryuma

Yizhi_Juruo约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年1月3日
更新
2026年1月3日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

AT_abc184_c [ABC184C] Super Ryuma题解

题目大意

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

  1. a+b=c+da+b=c+d 即处于主对角线上。
  2. a−b=c−da-b=c-d 即处于副对角线上。
  3. ∣a−c∣+∣b−d∣≤3|a-c|+|b-d|\leq3 即曼哈顿距离小于等于 33 。

思路分析

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

00 步

起点终点相同 r1=r2,c1=c2r_1=r_2,c_1=c_2 。

11 步

满足三种条件之一。

22 步

  • 曼哈顿距离 ≤6\leq 6 (可拆分为两步短距离移动,每步 ≤3\leq 3 ) ;
  • 主对角线差值的绝对值 ≤3\leq 3 ,或副对角线差值的绝对值 ≤3\leq 3 ;
  • 起点主对角线与终点副对角线的交点为整数坐标 ((r1−c1+r2+c2)((r_1-c_1+r_2+c_2) 为偶数 )) ,或起点副对角线与终点主对角线的交点为整数坐标 ((r1+c1+r2−c2)((r_1+c_1+r_2-c_2) 为偶数 )) ;

33 步

不满足上述条件。

AC Code

C++
#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;
}
原文