思路就是:分成四个区域, 一直递归细分, 考虑坐标的转化,转换公式:
要注意的是 a, b 传进去的时候要减1, 这样 0 就相当于 1, 取模就是会有其他为0的情况!!
/*//
Problem: 98. 分形之城
Contest: 递归与递推
URL: https://www.acwing.com/problem/content/100/
/*/
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;
const int inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 5;
struct node {
double x, y;
};
node solve(ll n, ll cnt) {
node now;
if (n == 0) {
now.x = 0, now.y = 0;
return now;
}
ll len = (ll)1 << (n - 1);
ll num = (ll)1 << (2 * n - 2);
node last = solve(n - 1, cnt % num);
int flag = cnt / num;
if (flag == 0) {
now.x = -last.y - len;
now.y = -last.x + len;
return now;
}
if (flag == 1) {
now.x = last.x + len;
now.y = last.y + len;
return now;
}
if (flag == 2) {
now.x = last.x + len;
now.y = last.y - len;
return now;
}
if (flag == 3) {
now.x = last.y - len;
now.y = last.x - len;
return now;
}
return now;
}
int main(int argc, char const *argv[]) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while (T --) {
ll n, a, b;
cin >> n >> a >> b;
node A = solve(n, a-1);
node B = solve(n, b-1);
double ans = 5 * (double)sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
printf("%.0lf\n", ans);
}
return 0;
}