Delta-wave HDU - 1030

Delta-wave

HDU - 1030
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.

InputInput contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).OutputOutput should contain the length of the shortest route.Sample Input
6 12 
Sample Output
3


OJ-ID:
hdu-1030

author:
Caution_X

date of submission:
20191010

tags:
规律题

description modelling:
给定图,问两个数字之间的“距离”

major steps to solve it:
将给定图分为3个图,两个数字之间的距离就是两个数字分别在3个图中的距离之和

warnings:

AC Code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,m;
    while(~scanf("%lld%lld",&n,&m)) {
        ll cn,cm,ln,lm,rn,rm;
        cn=ceil(sqrt(n));
        cm=ceil(sqrt(m));
        lm=(m-(cm-1)*(cm-1)-1)/2+1;
        ln=(n-(cn-1)*(cn-1)-1)/2+1;
        rm=(cm*cm-m)/2+1;
        rn=(cn*cn-n)/2+1;
        ll ans=0;
        ans=abs(cn-cm)+abs(ln-lm)+abs(rn-rm);
        printf("%lld\n",ans);
    }
    return 0;
}




猜你喜欢

转载自www.cnblogs.com/cautx/p/11651121.html