hdu 1030 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
主要找出规律,不说了,都是借鉴的!

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int leven=ceil(sqrt(n));
        int levem=ceil(sqrt(m));
        int rigtn=(n-(leven-1)*(leven-1)+1)/2;
        int rigtm=(m-(levem-1)*(levem-1)+1)/2;
        int leftn=(leven*leven-n)/2;
        int leftm=(levem*levem-m)/2;
       // cout<<leven<<" "<<rigtn<<" "<<leftn<<endl<<levem<<" "<<rigtm<<" "<<leftm<<endl;

        int sum=abs(levem-leven)+abs(rigtn-rigtm)+abs(leftn-leftm);
        cout<<sum<<endl;
    }
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值