题目链接
题意
给你一个分形图,及其构造规则。求n阶分形图的第x个位置和第y个位置的最近距离。
思路
每个图片可以分成四块,每次递归做好相应坐标转化即可。详见代码
代码
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
#define ll long long
#define pa pair<ll,ll>
pa cal(ll n, ll pos)
{
if(n == 0) return {1,1};
ll len = 1ll<<(n-1), blo = 1ll<<(2*n-2);
pa tmp = cal(n-1, pos%blo == 0 ? blo : pos%blo);
ll x = tmp.first, y = tmp.second, z = (pos-1)/blo;
if(z == 0) return {y,x};
if(z == 1) return {x,y+len};
if(z == 2) return {x+len,y+len};
if(z == 3) return {len+len+1-y,len+1-x};
}
int main()
{
ll t, x, y, n;
for(scanf("%lld",&t); t; --t)
{
scanf("%lld%lld%lld",&n,&x,&y);
pa a = cal(n,x);
pa b = cal(n,y);
// printf("%lld %lld - %lld %lld\n",a.first,a.second,b.first,b.second);
double ans = 10.0*sqrt(1.0*(a.first-b.first)*(a.first-b.first) + 1.0*(a.second-b.second)*(a.second-b.second));
printf("%.0f\n",ans);
}
return 0;
}