hdu 1030 Delta-wave(找规律)

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;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值