标签:cond most can 距离 变换 img eterm pos alt
With a growing desire for modernization in our increasingly larger cities comes a need for new street designs. Chris is one of the unfortunate city planners responsible for these designs. Each year the demands keep increasing, and this year he has even been asked to design a completely new city.
More work is not something Chris needs right now, since like any good bureaucrat, he is extremely lazy. Given that this is a character trait he has in common with most computer scientists it should come as no surprise that one of his closest friends, Paul, is in fact a computer scientist. And it was Paul who suggested the brilliant idea that has made Chris a hero among his peers: Fractal Streets! By using a Hilbert curve, he can easily fill in rectangular plots of arbitrary size with very little work.
A Hilbert curve of order 1 consists of one "cup". In a Hilbert curve of order 2 that cup is replaced by four smaller but identical cups and three connecting roads. In a Hilbert curve of order 3 those four cups are in turn replaced by four identical but still smaller cups and three connecting roads, etc. At each corner of a cup a driveway (with mailbox) is placed for a house, with a simple successive numbering. The house in the top left corner has number 1, and the distance between two adjacent houses is 10m.
The situation is shown graphically in figure 2. As you can see the Fractal Streets concept successfully eliminates the need for boring street grids, while still requiring very little effort from our bureaucrats.
As a token of their gratitude, several mayors have offered Chris a house in one of the many new neighborhoods built with his own new scheme. Chris would now like to know which of these offerings will get him a house closest to the local city planning office (of course each of these new neighborhoods has one). Luckily he will not have to actually drive along the street, because his new company "car" is one of those new flying cars. This high-tech vehicle allows him to travel in a straight line from his driveway to the driveway of his new office. Can you write a program to determine the distance he will have to fly for each offier (excluding the vertical distance at takeoff and landing)?
On the first line of the input is a positive integer, the number of test cases.
Then for each test case:
A line containing a three positive integers, n < 16 and h, o < \(2^{31}\), specifying the order of the Hilbert curve, and the house numbers of the offered house and the local city planning office.
For each test case:
One line containing the distance Chris will have to fly to his work in meters, rounded to the nearest integer.
发现,直接在求解n级城市中编号h的房屋和编号o的房屋之间的距离是及其困难的,我们将问题转化为求解编号h的房屋和编号o的房屋的二维坐标,设该图左上角坐标为\((0,0)\),房屋从0开始编号。 你觉得我会告诉你这是我为了方便处理的时候偷懒吗 ,如果已知一个点在n-1级城市所对应的坐标,那么根据构图的特殊性,我们是否能求解一个点在n级城市中的坐标。我们发现,对于n级城市,其包含的房屋数为\(2^{2*n}\),考虑在n级城市中的编号为p,那么它就是由(n-1)号城市中的编号\(p\%2^{2*n-2}\)变换而来,而根据\(p/2^{2*n-2}\)的大小我们还可以知道他在n级城市中是哪一部分。
假设我们已知在(n-1)号城市中p点是由(x,y)变换而来,讨论对于不同的\(p/2^{2*n-2}\)对(x,y)在城市从n-1级到n级时的坐标变换:
0 则代表其在n级城市中是左上角的那一部分,\((x,y)\rightarrow(y,x)\)
1 则代表其在n级城市中是右上角的那一部分,\((x,y)\rightarrow(x,y+2^{n-1})\)
2 则代表其在n级城市中是右下角的那一部分,\((x,y)\rightarrow(x+2^{n-1},y+2^{n-1})\)
3 则代表其在n级城市中是左下角的那一部分,\((x,y)\rightarrow(2^n-y-1,2^{n-1}-x-1)\)
以上坐标变换配合画图使用效果更佳!
知道了这些,我们便可以进行分治,众所周知,0级城市只有一个编号为0的房屋,坐标为\((0,0)\)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define pr pair < long long , long long >
#define mp make_pair
#define fi first
#define se second
using namespace std;
int N,T;
long long S,D;
long long Pow(long long x)
{
return x*x;
}
pr calc(long long P,int n)
{
if (n==0) return mp(0,0);
long long dis=1LL<<(n-1),num=1LL<<(2*n-2);
pr pos=calc(P%num,n-1);
long long x=pos.fi,y=pos.se;
if ((long long)(P/num)==0) return mp(y,x);
if ((long long)(P/num)==1) return mp(x,y+dis);
if ((long long)(P/num)==2) return mp(x+dis,y+dis);
if ((long long)(P/num)==3) return mp(dis*2-y-1,dis-x-1);
}
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%lld%lld",&N,&S,&D);
S--;D--;//坐标和编号都从0开始
pr p1=calc(S,N);
pr p2=calc(D,N);
double ans=sqrt(Pow(p1.fi-p2.fi)+Pow(p1.se-p2.se));
printf("%lld\n",(long long)(ans*10+0.5));
}
return 0;
}
标签:cond most can 距离 变换 img eterm pos alt
原文地址:https://www.cnblogs.com/Code-Geass/p/9804645.html