您的位置:首页 > 其它

hdu 1030 Delta-wave(找规律)

2017-03-12 08:18 435 查看

Delta-wave

Problem Description

A triangle field is numbered with successive integers in the way shown on the picture below.

The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller’s route.

Write the program to determine the length of the shortest route connecting cells with numbers N and M.

Input

Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).

Output

Output should contain the length of the shortest route.

Sample Input

6 12

Sample Output

3

[参考博客]:(http://www.cnblogs.com/ACMan/archive/2012/05/30/2526798.html

ps:分析一下图可以发现,这张图可以从三个角度来看



而题目的答案就是3个图上2个点之间的层数的高度差之和

例如 6 12 ,level=1,left=1,right=1,答案就是3。

例如 3 12 ,level=2,left=1,right=2,答案就是6。

下面是看了别人的思路之后找的规律

代码:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
double n,m;
while(~scanf("%lf%lf",&n,&m))
{
int x1,y1,z1,x2,y2,z2;//x=level,y=left,z=right
if((int)sqrt(n)==sqrt(n))
x1=sqrt(n);
else
x1=sqrt(n)+1;
if(n==((int)sqrt(n))*((int)sqrt(n)))
y1=sqrt(n);
else
y1=(n-((int)sqrt(n))*((int)sqrt(n))-1)/2+1;
if(n==((int)sqrt(n))*((int)sqrt(n)))
z1=1;
else
z1=(((int)sqrt(n)+1)*((int)sqrt(n)+1)-n)/2+1;
if((int)sqrt(m)==sqrt(m))
x2=sqrt(m);
else
x2=sqrt(m)+1;
if(m==((int)sqrt(m))*((int)sqrt(m)))
y2=sqrt(m);
else
y2=(m-((int)sqrt(m))*((int)sqrt(m))-1)/2+1;
if(m==((int)sqrt(m))*((int)sqrt(m)))
z2=1;
else
z2=(((int)sqrt(m)+1)*((int)sqrt(m)+1)-m)/2+1;
printf("%d\n",abs(x1-x2)+abs(y1-y2)+abs(z1-z2));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: