POJ3889【Fractal Streets】(递归与回溯)

链接:http://poj.org/problem?id=3889

题目描述:
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)?

输入描述:
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.

输出描述:
For each test case:
One line containing the distance Chris will have to fly to his work in meters, rounded to the nearest integer.

输入样例:

3
1 1 2
2 16 1
3 4 33

输出样例:

10
30
50

题意:
计算n阶分形街道中给定两点的直线距离

数据范围:
n < 16,两点 h,o < 2^31

思路:
先用递归函数求出起点和终点坐标
再用距离公式求出答案
分析递归:
n = 1时
第一个点坐标(1,1)
第二个点坐标(1,2)
第三个点坐标(2,2)
第四个点坐标(2,1)
n > 1时
在右上角和右下角各放置一个n-1
在左上角放置顺时针旋转90度的n-1
在左下角放置逆时针旋转90度的n-1

代码:

#include<cstdio>
#include<cmath>
typedef long long ll;
ll p[40];//代表第n级分形图编号总数 
void rec(ll n,ll s,ll &x,ll &y)//第n级编号为s坐标(x,y) 
{
    if(n==1)
    {
        if(s==1)x=1,y=1;
        else if(s==2)x=1,y=2;
        else if(s==3)x=2,y=2;
        else x=2,y=1;
        return;
    }
    if(s<=p[n-1])rec(n-1,s,y,x);
    else if(s<=2*p[n-1])rec(n-1,s-p[n-1],x,y),y+=(1<<n-1);
    else if(s<=3*p[n-1])rec(n-1,s-2*p[n-1],x,y),x+=(1<<n-1),y+=(1<<n-1);
    else rec(n-1,s-3*p[n-1],y,x),x=(1<<n)+1-x,y=(1<<n-1)+1-y;
}
int main()
{
    int t;
    ll n,s,e,sx,sy,ex,ey;
    scanf("%d",&t);
    p[1]=4;
    for(int i=2;i<=33;i++)p[i]=4*p[i-1];
    while(t--)
    {
        scanf("%lld%lld%lld",&n,&s,&e);
        rec(n,s,sx,sy);//分别求出s和e的坐标 
        rec(n,e,ex,ey);
        printf("%.0f\n",sqrt((sx-ex)*(sx-ex)+(sy-ey)*(sy-ey))*10);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔说给风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值