C - 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 < 2 31, 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

题意:给你图形阶数和两个点的编号,问你两点之间的直线距离,输出为整数(结果四舍五入即可)

思路:根据题目可知图形的规律,N阶图形分为四部分,右上角与右下角和N-1阶图形一样,左上角顺时针旋转90度为N-1阶图形

,左下角逆时针旋转90度为N-1阶图形,递归从N-1阶图形得到N阶图形编号为m的点的坐标即可。若不知道规律,自己取多组N-1阶和N阶图形的同一点坐标进行分析比较就行。

PS:从一个点到相邻点的距离是10

只要耐心做就可以做出来,之前不知道为什么算两点距离时爆了abs函数,导致编译错误……

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll; 
const int MAX = 1e5;
const int mod = 9901;
pair<ll,ll> calc(int n,ll m){
	if(n==1){//横轴为x,数轴为y,左上角为原点 
		if(m==1) return make_pair(1,1);
		else if(m==2) return make_pair(2,1);
		else if(m==3) return make_pair(2,2);
		else return make_pair(1,2);
	}
	pair<ll,ll> ans;
	int pre_num=pow(2,2*(n-1));//n-1阶图形里有多少点(或者说房子) 
	int x=(m-1)/pre_num,pre_index=m%pre_num;
	if(!pre_index) pre_index=pre_num; 
	if(x==0||x==3){
		pre_index=pre_num+1-pre_index;
	}
	ans=calc(n-1,pre_index);
	ll add_distance=pow(2,n-1);//n-1阶图形的边长
	if(x==1||x==2){
		ans.first+=add_distance;
		if(x==2){
			ans.second+=add_distance;
		}
	}
	else{
		ll pre_x=ans.first,pre_y=ans.second;
		//坐标关系自己找多对点进行比较就会发现规律 
		if(x==0){
			ans.second=pre_x;
			ans.first=add_distance+1-pre_y;	
		}
		if(x==3){
			ans.first=pre_y;
			ans.second=2*add_distance+1-pre_x;
		}
	}
	return ans;
}
ll dis(pair<ll,ll> ans1,pair<ll,ll> ans2){
	double ans;
	ans=100*sqrt((ans1.first-ans2.first)*(ans1.first-ans2.first)+(ans1.second-ans2.second)*(ans1.second-ans2.second));
	return (ll)ans;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,h,o;
		scanf("%d%d%d",&n,&h,&o);
		pair<ll,ll> ans1=calc(n,h),ans2=calc(n,o);
		//cout<<ans1.first<<" "<<ans1.second<<endl;
		//cout<<ans2.first<<" "<<ans2.second<<endl;
		ll final = dis(ans1,ans2);
		if(final%10>4) final=final/10+1;
		else final=final/10;
		printf("%lld\n",final);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值