POJ3889 Fractal Streets
POJ3889 Fractal Streets
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)?
Input
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 < 231, specifying the order of the Hilbert 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 will have to fly to his work in meters, rounded to the nearest integer.
Sample Input
3
1 1 2
2 16 1
3 4 33
Sample Output
10
30
50
递归
注意:1<<(2n-2)会超出int范围应该用1LL<<(2n-2)
30分到100分的惨痛经历
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> using namespace std; long long h,o,f,t,xx1,yy1; void calc(int N,long long m) { if(N==1) { if(m==0)xx1=0,yy1=1; if(m==1)xx1=1,yy1=1; if(m==2)xx1=1,yy1=0; if(m==3)xx1=0,yy1=0; return ; } calc(N-1,m%(long long)(1LL<<(2*N-2))); long long c=m/ 3ff7 (long long)(1LL<<(2*N-2)),r=(long long)(1LL<<(N-1)),tt1=xx1,tt2=yy1; if(c==0)xx1=r-tt2-1,yy1=r*2-tt1-1; if(c==1)xx1=xx1+r,yy1=yy1+r; if(c==2)xx1=xx1+r,yy1=yy1; if(c==3)xx1=tt2,yy1=tt1; return ; } void sol() { cin>>h>>o>>f; calc(h,o-1); double a=xx1,b=yy1; calc(h,f-1); double a1=xx1,b1=yy1; long long in=(double) sqrt((a-a1)*(a-a1)+(b-b1)*(b-b1))*10+0.5; cout<<in<<endl; } int main() { cin>>t; while(t>0){t--;sol();} return 0; }
- 【POJ 3889】Fractal Streets
- POJ Function Run Fun
- POJ 2828 线段树
- poj2251
- Poj 2255 Tree Recovery
- POJ 2299 解题报告
- poj1716 Integer Intervals(差分约束)
- poj 1509 Glass Beads【最小表示法】
- poj 3320 尺取法
- POJ 1062 昂贵的聘礼 (最短路应用 Dijkstra算法)
- POJ 1077 Eight(八数码第四境界|双向广搜+哈希)
- POJ 3678 Katu Puzzle(强连通 法)
- poj 2373 dp单调队列优化
- poj 3608 Bridge Across Islands (计算几何)
- poj 3525 Most Distant Point from the Sea(半平面交+二分)
- poj 1062 昂贵的聘礼 (dfs||最短路)
- poj 1845
- POJ 2657 Comfort(模拟+dfs)
- Collecting Bugs POJ - 2096 期望dp
- POJ 1150 The Last Non-zero Digit