Fractal Streets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 445   Accepted: 162

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 < 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

Source

 
丁队长告诉我们,心情不好的时候就要做做这种题
 
李煜东题解(侵删):
š关键是要解决calc(N,P,M) :求编号为M的房屋在旋转P度的N级城市中的位置(P=0,90,180或270,逆时针为正方向)。本题就是求calc(N,0,S)与calc(N,0,D)的距离。
š记一个旋转P度的N级城市为city(N,P)。当N>1时,该城市由四部分组成,左上是city(N-1,P-90),左下是city(N-1,P+90),右上和右下是city(N-1,P)。
š已知N与P后很容易算出该城市道路依次经过其四个部分的顺序,通过M在1~22N四等分的大小位置可以确定M位于哪一部分(记为P’)。把之前经过的几个部分的房屋数量从M中减去(记为M’),然后递归求解calc(N-1,P’,M’)即可。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath> inline void read(long long &x)
{
x = ;char ch = getchar(),c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const long long INF = 0x3f3f3f3f; long long t, N, S, D, pow4[], pow2[]; struct Node
{
long long x, y;
Node(long long _x, long long _y){x = _x,y = _y;}
Node(){}
}; //求解编号为m的房子,在逆时针旋转p度后的n级城市中的坐标
//p只能取值0,90,270
//90,270编号反转,0不变 Node cal(long long n, long long p, long long m)
{
if(n == )
{
if(p == )
{
if(m == )return Node(,);
else if(m == ) return Node(,);
else if(m == ) return Node(,);
else return Node(,);
}
else if(p == )
{
if(m == )return Node(,);
else if(m == ) return Node(,);
else if(m == ) return Node(,);
else return Node(,);
}
else if(p == )
{
if(m == )return Node(,);
else if(m == ) return Node(,);
else if(m == ) return Node(,);
else return Node(,);
}
else if(p == )
{
if(m == )return Node(,);
else if(m == ) return Node(,);
else if(m == ) return Node(,);
else return Node(,);
}
}
Node tmp;
long long a = pow4[n - ];
long long r = (m - ) / a + ;
if(p == )
{
if(r == ) tmp = cal(n - , , m);
else if(r == ) tmp = cal(n - , , m - a), tmp.y += pow2[n - ];
else if(r == ) tmp = cal(n - , , m - * a), tmp.x += pow2[n - ], tmp.y += pow2[n - ];
else tmp = cal(n - , , m - a * ), tmp.x += pow2[n - ];
}
else if(p == )
{
if(r == ) tmp = cal(n - , , m), tmp.x += pow2[n - ], tmp.y += pow2[n - ];
else if(r == ) tmp = cal(n - , , m - a), tmp.y += pow2[n - ];
else if(r == ) tmp = cal(n - , , m - a * );
else tmp = cal(n - , , m - a * ), tmp.x += pow2[n - ];
}
else if(p == )
{
if(r == ) tmp = cal(n - , , m);
else if(r == ) tmp = cal(n - , , m - a), tmp.x += pow2[n - ];
else if(r == ) tmp = cal(n - , , m - * a), tmp.x += pow2[n - ], tmp.y += pow2[n - ];
else tmp = cal(n - , , m - * a), tmp.y += pow2[n - ];
}
else if(p == )
{
if(r == ) tmp = cal(n - , , m), tmp.x += pow2[n - ], tmp.y += pow2[n - ];
else if(r == ) tmp = cal(n - , , m - a), tmp.x += pow2[n - ];
else if(r == ) tmp = cal(n - , , m - a * );
else tmp = cal(n - , , m - a * ), tmp.y += pow2[n - ];
}
return tmp;
} int main()
{
//freopen("data.txt", "r" ,stdin);
read(t);
pow4[] = , pow2[] = ;
for(register int i = ;i <= ;++ i) pow4[i] = (pow4[i - ] << ), pow2[i] = (pow2[i - ] << );
Node tmp1, tmp2;
for(;t;-- t)
{
read(N),read(S),read(D);
tmp1 = cal(N, , S);
tmp2 = cal(N, , D);
double len = sqrt((long long)abs(tmp1.x - tmp2.x) * abs(tmp1.x - tmp2.x) + (long long)abs(tmp1.y - tmp2.y) * abs(tmp1.y - tmp2.y)) * ;
printf("%lld\n", (long long)(len + 0.5));
}
return ;
}

POJ3889

POJ3889Fractal Streets的更多相关文章

  1. POJ 3889 Fractal Streets(逼近模拟)

    $ POJ~3889~Fractal~Streets $(模拟) $ solution: $ 这是一道淳朴的模拟题,最近发现这种题目总是可以用逼近法,就再来练练手吧. 首先对于每个编号我们可以用逼近法 ...

  2. Codeforces 1070J Streets and Avenues in Berhattan dp

    Streets and Avenues in Berhattan 我们首先能发现在最优情况下最多只有一种颜色会分别在行和列, 因为你把式子写出来是个二次函数, 在两端取极值. 然后我们就枚举哪个颜色会 ...

  3. One-Way Streets (oneway)

    One-Way Streets (oneway) 题目描述 Once upon a time there was a country with nn cities and mm bidirection ...

  4. poj3889 fractal streets

    分形街道 我干,这个毒瘤. 想起来就头痛. 首先看题就是一大难题...... 说一下题目大意吧. 每当n+1时,把n阶图复制为4份.2*2排好. 右边两个不动.左上顺时针旋转90°,左下逆时针旋转90 ...

  5. Luogu4652 CEOI2017 One-Way Streets 树上差分

    传送门 题意:给出$N$个点.$M$条无向边的图,现在你需要给它定向,并满足$Q$个条件:每个条件形如$(x_i,y_i)$,表示定向之后需要存在路径从$x_i$走向$y_i$.问每条边是否都有唯一定 ...

  6. CF 1070J Streets and Avenues in Berhattan

    DP的数组f其实开得不够大,应该开200000,但是它在cf上就是过了... 题意是把一堆字母分别分配到行和列. 分析一下,答案实际上只和n行中和m列中每种字母分配的个数有关.而且答案只和" ...

  7. bzoj2263: Pku3889 Fractal Streets

    给出两点编号,求如图所示的图中两点间欧氏距离*10取整 递归处理由编号求出坐标 #include<cstdio> #include<cmath> int T,n,s,t; vo ...

  8. 【刷题】LOJ 2480 「CEOI2017」One-Way Streets

    题目描述 给定一张 \(n\) 个点 \(m\) 条边的无向图,现在想要把这张图定向. 有 \(p\) 个限制条件,每个条件形如 \((xi,yi)\) ,表示在新的有向图当中,\(x_i\) 要能够 ...

  9. CodeForces 1070J Streets and Avenues in Berhattan 性质+动态规划

    题目大意: 你有$k$个数,分为$26$种 对于每个数,你可以选择选进$A$集合或者$B$集合或者不选 要求$A$集合中必须有$n$个数,$B$集合中必须有$m$个数 记第$i$种数在$A$集合中的个 ...

随机推荐

  1. 732F Tourist Reform

    // CF 732F Tourist Reform // 思路:两遍tarjan // 找强联通分量 #include <bits/stdc++.h> using namespace st ...

  2. qq音乐网站页面切换歌手分类时不刷新

    1.提交表单时会自动刷新页面(提交表单一般使用post方式提交) 2.动态加载数据时页面不会刷新,只是把页面中某个位置的内容替换掉想要的内容 3.一般在切换到不同的html页面时才会强制让你把页面刷新 ...

  3. 搭建nodejs代理服务器,从而解决跨域问题

    先在同级处新建js文件(app.js) 使用时npm 安装 Node.js 模块语法 也就是对应的文件所在地“npm install”一下 然后安装对应需要的模块: expresspathreques ...

  4. x86架构:x86架构

    ylbtech-x86架构:x86架构 X86架构(The X86 architecture)是微处理器执行的计算机语言指令集,指一个intel通用计算机系列的标准编号缩写,也标识一套通用的计算机指令 ...

  5. 使用jstl el表达式对form表单的功能进行区分 比如新建和修改共用一个form

    新建一个专栏,修改这个专栏信息 完全可以做在一个jsp的一个form中 但是,需要注意的是,使用mvc的对象属性自动封装的话 如果id为空,将会报错,无法进入controller中的 所以要在页面上判 ...

  6. Webpack构建前端项目

    前言 公司据说要搞前后端分离,趁这两天项目完成的差不多,抓紧时间学习一下前端知识 现在流行前端项目工程化,那么第一个问题就是如何创建工程(项目),第一次玩webpack 通过 NPM 创建项目 # 创 ...

  7. videojs使用的常见问题

    1.报错The play() request was interrupted by a new load request 我在动态更换video的url时会报这个错.修改一下原来的代码如下,就正常了 ...

  8. qq邮箱问卷,测试不支持form表单

    想做个类似苹果调查问卷的: 找到qq邮箱的代码编辑器: 写好我们的网页(h5) <!DOCTYPE html> <html lang="en"> <h ...

  9. LeetCode409Longest Palindrome最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...

  10. 关于distinct 和group by的去重逻辑浅析

    在数据库操作中,我们常常遇到需要将数据去重计数的工作.例如: 表A,列col A C A B C D A B 结果就是一共出现4个不同的字母A.B.C.D 即结果为4 大体上我们可以选择count(d ...