HDU:1030 Delta-wave

Delta-wave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8736    Accepted Submission(s): 3479
题目链接

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
题意求:给出两个数M,N,求由M到N所穿过的最少边数。
解题思路链接: 解题思路链接
#include<iostream>
#include<stdio.h>
#include<set>
#include<string.h>
#include<cmath>

using namespace std;

int main()
{
    long long int start,last;
    while(~scanf("%lld%lld",&start,&last))
    {
        int level1 = (int)sqrt(start);
        if(start > level1*level1) level1++;
        int level2 = (int)sqrt(last);
        if(last > level2*level2) level2++;
        int left1 = start-(level1-1)*(level1-1);
        if(left1%2)  ///是奇数列
            left1 = left1/2+1;
        else
            left1 = left1/2;
        int left2 = last-(level2-1)*(level2-1);
        if(left2%2)
            left2 = left2/2+1;
        else
            left2 = left2/2;
        int right1 = level1*level1-start+1;
        if(right1%2)
            right1 = right1/2+1;
        else
            right1 = right1/2;
        int right2 = level2*level2-last+1;
        if(right2%2)
            right2 = right2/2+1;
        else
            right2 = right2/2;
        int ans = abs(left1-left2)+abs(level1-level2)+abs(right1-right2);
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值