UVA 10233 Dermuba Triangle

来源:互联网 发布:订餐的软件 编辑:程序博客网 时间:2024/06/14 03:57

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1174

Dermuba Triangle is the universe-famous flat and triangularregion in the L-PAX planet in the Geometriagalaxy. Actually nobody knows whether the region istriangular or how it came into existence or how big itis. But the people of Dermuba firmly believe that theregion extends to infinity. They live in equilateral triangularfields with sides exactly equal to 1km. Housesare always built at the circumcentres of the triangularfields. Their houses are numbered as shown in thefigure on the right.

这里写图片描述

When Dermubian people wishes to visit each other,they follow the shortest path from their house to thedestination house. This shortest path is obviously thestraight line distance that connects these two houses.Moreover, they also visit all the houses that lie in the straight line they travel. Now, comes your task.You have to write a program which computes the length of the shortest path between two houses giventheir house numbers.

Input

Input consists of several lines with two non-negative integer values n and m which specify the start anddestination house numbers. 0 ≤ n, m ≤ 2147483647. Actually, there are houses beyond this region,but some seventh-sense people in Dermuba say that these houses are left for the dead people.

Output

For each line in the input, print the shortest distance between the given houses in kilometers roundedoff to three decimal places.

Sample Input

0 7

2 8

9 10

10 11

Sample Output

1.528

1.528

0.577

0.577

提示

题意:

给出两个数字,求出两个对应数字边长为1的等边三角形的中心距离。

思路:

先建立坐标系以0为原点,利用三角形(等边)中心的特性,中心到边长的距离为中心到定点距离的一半。因此中心到边长的距离为sqrt(3)/6,到顶点的距离为sqrt(3)/3。先假设每增加一行y坐标增加sqrt(3)/2的距离,若为倒三角则要多减sqrt(3)/6,上面这些值怎么来的就留给你去想。x轴呢我们从图中可看出可得到相对于编号0三角形每多一个三角形在x轴上就增加二分之一的距离(先不考虑左右对正负号的影响),那么我们规定向左偏要减,向右偏要加,我们需要先求出该数字所在行最中间的数字,才容易求x坐标。这样一个坐标系就建好了,之后求两点间距离里即可。

倒三角的判定:从图中可以看出行号(从0开始计数)与数字之和是奇数则该三角形是倒三角。

示例程序

#include <stdio.h>#include <math.h>#define gen3 sqrt(3)struct point{    double x,y;};struct point getpoint(int n){    int i;    long long t,t1;    struct point pos;    t=sqrt(n);//这里的t表示该数字所在的行,从0开始计数    pos.y=t*gen3/2;    if((t+n)%2==1)//从图中可以看出行号加数字之和是奇数则该三角形是倒三角    {        pos.y=pos.y-gen3/6;    }    t++;    t=t*t;//这里计算的是该数字所在行数的下一行,最左边的数,便于求位于当前行最中间的那个值,利用它来求出该行相对于编号0三角形x坐标的距离    t1=0;    for(i=1;t>=t1+2*i;i++)    {        t1=t1+2*i;    }    pos.x=(n-t1)*0.5;//计算在x轴上与编号0三角形的相对位置,左偏右偏也不需要我们去特判    return pos;}double dis(struct point a,struct point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){    int n,m;    while(scanf("%d %d",&n,&m)!=EOF)    {        printf("%.3lf\n",dis(getpoint(n),getpoint(m)));    }    return 0;}
1 0
原创粉丝点击