Fractal Streets poj3889

Urban planning is a big problem in urban construction.

Unfortunately, many cities at the beginning of the construction is not a good plan, after the expansion of the city planning and inadequate problem began to appear.

The city called Fractal idea of ​​such a plan, as shown below:

city.png

After the expansion of urban scale, Fractal solution is the same as the original urban structure of the region in accordance with the figure a way around the building in the city, enhance the level of the city.

For any level of the city, we start from the top left square blocks in accordance with the road grade.

Although this program sucks, staff Fractal planning department still want to know, if the city developed to the level of N, numbered linear distance of two blocks A and B is.

Refers to a block from the distance between the center blocks, each block side length is 10 meters square.

Input Format

The first line of the input integer n- n-, represents the number of test data.

The following n n lines, n sets of test input data, each line.

Each set of data includes three integers  N , A , B N, A, B, represents the city level, and two blocks of numbers, separated by spaces between integers.

Output Format

Total output n- n-rows, each row corresponds to a set of the output test data, the result is rounded to the nearest integer.

data range

1N311≤N≤31,
1A,B22N1≤A,B≤22N,
1n10001≤n≤1000

Sample input:

3 
1 1 2 
2 16 1 
3 4 33 

Sample output:

10 
30 
50 

The basic pattern designated ⭐ Note: While the reference numerals in FIG grid, in fact, refers to the upper left corner of the grid point (a coordinate point)

                  

FIG. 4 

FIG. 4 becomes the direction in the direction of the upper left corner, the only x, y coordinates interchanged, i.e. in FIG. 4 (x, y) becomes (y, x)

 Figure 5

Into the upper right corner, as long as the FIG. 4 (x, y) becomes (x, y + len) len (this layer is half the side length of the city)

 

 

 Figure 6

Lower right corner of FIG. 4 (x, y) becomes (x + len, y + len)

 

 

 Figure 7

The lower left corner, the hardest, several transformation unit (x, y) - "(x, y - (len - 1)) -" (- (y - len + 1), - x) i.e. (len - y - 1 , -x) - "(len - y + len, (len - 1) - x)

           

 

 

 Special attention first to the left so that y - len, but! ! ! ! We want the coordinates of the upper right corner is the origin, so that y -len + 1, then why not y - len + (len / 2) it?

 Because although we are divided into upper left, upper right, lower left, lower right, in fact, every little may also be subdivided into four, our aim is to make the upper right corner of the coordinate origin (all other small houses around it coordinate conversion) so the translation from a unit 1, instead of (len / 2) this is equivalent to the upper right conversion (and the coordinates of the upper right are so to be converted, can not be len / 2)

⭐注意:图中虽然标号在格子内,其实指的是格子左上角的点(是一个坐标点)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define P pair<ll, ll>
 5 
 6 int t, n;
 7 
 8 P calc(int n, ll m)
 9 {
10     if (!n) return {0, 0};
11     ll len =1ll << (n - 1), cnt = 1ll << ((n << 1) - 2); 12 P a = calc(n - 1, m % cnt); 13 ll z = m / cnt, x = a.first, y = a.second; 14 if (!z) return {y, x}; 15 if (z == 1) return {x, y + len}; 16 if (z == 2) return {x + len, y + len}; 17 return {(len << 1) - y - 1, len - 1 - x}; 18 } 19 20 int main() 21 { 22 scanf("%d", &t); 23 while(t--) 24  { 25  ll a,b; 26 scanf("%d%lld%lld", &n, &a, &b); 27 P x = calc(n, a - 1); 28 P y = calc(n, b - 1); 29 ll dx = x.first - y.first, dy = x.second - y.second; 30 double ans = (sqrt(dx * dx + dy * dy) * 10); 31 printf("%0.lf\n",ans); 32  } 33 return 0; 34 }

 

 

 

Guess you like

Origin www.cnblogs.com/2aptx4869/p/12417095.html