【OJ】POJ3889 Fractal Streets (分形,递归)

Language:Default
Fractal Streets
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 1195Accepted: 349

Description

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

Source

题解

这又是《算法竞赛进阶指南》的一道例题。思路没什么难的,就是递归。但是,问题在于细节。
根据子图形的入口坐标,找入口坐标(比如图1左上角)。有很多次旋转翻转,很容易把自己绕晕。其实,只要记住直角坐标系的旋转90度公式,以及关于坐标轴翻转的公式,就没那么难了。

(x,y)经过多次关于原点90度旋转变换,以及关于坐标轴翻转后,变为(a,b),它一定满足 ( ∣ x ∣ = = ∣ a ∣ 且 ∣ y ∣ = = ∣ b ∣ ) ( |x|==|a| 且 |y|==|b|) (x==ay==b) ( ∣ x ∣ = = ∣ b ∣ 且 ∣ y ∣ = = ∣ a ∣ ) ( |x|==|b| 且 |y|==|a|) (x==by==a),只要记住这点,不管之前怎么绕,最后大概看一下,就能得到最后的坐标

另外,还有一个麻烦的地方是四舍五入取整。
输出四舍五入取整的方法有

//C stdio
printf("%.0lf\n",double_value);

//C++ iostream,iomanip
cout.setf(ios::fixed);//定点数输出 
cout<<setprecision(0)<<double_value<<endl; //iomanip头文件

如果要获得四舍五入的整型结果,那要这样写:

#define EPS 1e-12
int int_round(double x)
{
	//注意(int)是向0取整的
    return (x>=0.0) ? (int)(x+0.5+EPS):(int)(x-0.5-EPS);
}

代码

#include <iostream>
#include <utility>
#include <algorithm>
#include <cmath>

#define EPS 1e-12

using namespace std;

pair<int, int> locate(int level, int id)
{
    if(level == 0)return make_pair(0, 0);
    int sub_n = 1 << (level - 1), sub_cnt = sub_n * sub_n;
    pair<int, int> sub_pos = locate(level - 1, id % sub_cnt);
    int ri = sub_pos.first, ci = sub_pos.second; //r:row, c:column
    int z = id / sub_cnt;
    if(z == 0) return make_pair(ci, ri);
    if(z == 1) return make_pair(ri, ci + sub_n);
    if(z == 2) return make_pair(ri + sub_n, ci + sub_n);
    if(z == 3) return make_pair(-ci + sub_n + sub_n - 1, -ri + sub_n - 1);
    else return make_pair(-1, -1);
}

int int_round(double x)
{
    double tmp = round(x); //有一些老的编译器不支持
    //double tmp =(x >= 0.0) ? floor(x + 0.5 + EPS) : ceil(x - 0.5 - EPS);
    //return x>=0 ? (int)(tmp + EPS) : (int)(tmp - EPS);
    return (x>=0.0) ? (int)(x+0.5+EPS):(int)(x-0.5-EPS);
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n, a, b;
        cin >> n >> a >> b;
        pair<int, int> pa = locate(n, a - 1), pb = locate(n, b - 1);
        int ra = pa.first, ca = pa.second, rb = pb.first, cb = pb.second;
        double tmp_ans= 10*sqrt((double)(((ra - rb) * (ra - rb) + (ca - cb) * (ca - cb)))); 
        //printf("%.0f\n",tmp_ans);
        int ans=int_round(tmp_ans);
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值