HDU Delta-wave

Delta-wave

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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

Source

这是一个三角形每层所容纳的数加2,且第i层共容纳i*i各数。求第n个数到第m个数需要几步(只能通过2个三角形的公共边穿过)。
不难发现从第i1层穿到第i2层最少需要abs(i1-i2)步,经过大量的数据统计发现以◇为坐标计算的话步数量为abs(x1-x2)+abs(y1-y2)+abs(i1-i2)
ps:这道题是我这次作业最后做的代码一次ac感觉真棒!
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
    int n,m;
    while(cin>>n>>m){
        int t1,t2,i;
         t1=sqrt(n);
         if(t1<sqrt(n))t1++;
         t2=sqrt(m);
         if(t2<sqrt(m))t2++;
         if(n==1)t1=1;
         if(m==1)t2=1;
         int x1=n,x2=m,y1=n,y2=m;
         x1-=(t1-1)*(t1-1);
         x2-=(t2-1)*(t2-1);
         y1=x1;
         y2=x2;
         if(x1%2!=0)x1++;
         x1/=2;
         if(x2%2!=0)x2++;
         x2/=2;
         y2/=2;
         y2++;
         y1/=2;
         y1++;
         y2=t2-y2;
         y1=t1-y1;
         int sum=0;
         cout<<(sum=abs(x1-x2)+abs(t1-t2)+abs(y1-y2))<<'\12';
    }
    return 0;
}

运行结果:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值