Description
With a growing desire for modernization in we increasingly larger cities comes a need for new street designs. Chris is one of the unfortunate city planners responsible for these designs. The demands keep increasing, and this year he had even been asked to design a completely new city.
More work isn't something Chris needs right now, since like any good bureaucrat, he's extremely lazy. Given that's a character trait he have in common with most computer scientists it should come as no surprise that one Of his closest friends, Paul, was in fact a computer scientist. And it was Paul suggested, the brilliant idea, have made Chris a hero among his peers:fractal streets! By using a Hilbert curve, the 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 was replaced by four smaller but identical cups and three connecting roads. In a Hilbert curve of order 3 those four cups is in turn replaced by four identical but still smaller cups and three Conn Ecting roads, etc. At all corner of a cup a driveway (with mailbox) are placed for a house, with a simple successive numbering. The house in the top left corner have number 1, and the distance between the adjacent houses is 10m.
The situation is shown graphically in Figure 2. As can see the Fractal Streets concept successfully eliminates the need for boring street grids and while still requiring Very little effort from our bureaucrats.
As a token of their gratitude, several mayors has offered Chris a house in one of the many new neighborhoods built with H is own new scheme. Chris would now like to know which of these offerings would get him a house closest to the local city planning office (of C Ourse each of the these new neighborhoods has one). Luckily he would not be actually drive along the street, because his new company "Car" is one of the those new flying cars . This High-tech vehicle allows him-to-travel-a-straight-from-driveway-to-driveway of his new office. Can Write a program to determine the distance he'll has to fly for each offier (excluding the vertical distance at Takeoff and landing)?
Input
The first line of the input is a positive integer, the number of the test cases.
Then to each test case:
A line containing a three positive integers, n < h, o < \ (2^{31}\), specifying the order of the Hilb ERT Curve, and the house numbers of the offered house and the local city planning office.
Output
For each test case:
One line containing the distance Chris would has to fly to the meters and rounded to the nearest integer.
Solution
It is found that the distance between the house with the number h in the N-class city and the house numbered O is difficult, and we turn the problem into the two-dimensional coordinates of the house with number H and the house numbered O, and the upper left corner coordinates\ ((0,0) \), the house is numbered starting from 0.you think I'm going to tell you that I'm slacking off for the sake of convenience?, if a point is known to correspond to the coordinates of a n-1 city, then whether we can solve the coordinates of a point in an N-class city according to the particularity of the composition. We found that for n-tier cities, the number of houses included\ (2^{2*n}\), consider the number in the N-tier city as P, then it is the number in the city of (n-1)\ (p\%2^{2*n-2}\)Transformed, and according to\ (p/2^{2*n-2}\)Size we can also tell which part of the N-tier city he is in.
Suppose we know that P-points are transformed by (x, y) in the City of (n-1) and discussed for different\ (p/2^{2*n-2}\)The coordinate transformation for (x, y) in cities from n-1 level to n level:
0 means that it is the upper-left part of the N-class city,\ ((x, y) \rightarrow (y,x) \)
1 means that it is in the upper right corner of the N-class city,\ ((x, y) \rightarrow (x,y+2^{n-1}) \)
2 means that it is in the lower right corner of the N-tier city,\ ((x, y) \rightarrow (x+2^{n-1},y+2^{n-1}) \)
3 means that it is the lower-left part of the N-class city,\ ((x, y) \rightarrow (2^n-y-1,2^{n-1}-x-1) \)
The above coordinate transformation works better with drawing!
Knowing this, we can divide and conquer, as we all know, the 0-tier city has only a house numbered 0, coordinates\ ((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 secondusing 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--;//coordinates and numbering starting from 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;}
POJ3889 Fractal Streets