Hdu 1030 Delta Wave

来源:互联网 发布:淘宝产品怎么排名靠前 编辑:程序博客网 时间:2024/06/10 03:56

【比赛提醒】BestCoder 你报名了吗?(点击报名) 
【科普】什么是BestCoder?如何参加?

Delta-wave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5770    Accepted Submission(s): 2197


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 的步数


解题方法:分别 计算 在数 M ,N  到顶部 到 左边 到 右边的距离  

      然后计算M N之间的距 只需要 他们顶部 到左边, 到右边的距离差的绝对值 之和 就是M N之间的距离

代码:

#include<iostream>#include<cmath>#include<cstdio>#include<cstdlib>using namespace std;struct node{    int x, y, z;    //x 为到顶部的距离 y 为到左边的距离 z 为到右边的距离};node ssd(int k){    int x = sqrt(k-1.0); //这其实是求上一层的    int y = (k-x*x-1)/2;    int z =((x+1)*(x+1)-k)/2;    node t;    t.x = x;    t.y = y;    t.z = z;    return t;}int main(){    int n , m;    while(scanf("%d%d", &n, &m)!= EOF)    {        //题目大意 输入两个数 A B,这两个数是三角形里面的, 问 A到B的最短路        // 第i行的第 1 个数是 (i-1)^2 + 1 最后一个数是 i^2        //给出m , n 那么他么分别在 ceil(sqrt(n))层        //要求最短距离思路是 到上的层数 到左边的层数, 到右边的层数 之和就是所求        node s, g;        s = ssd(n);        g = ssd(m);        printf("%d\n", abs(s.x-g.x)+ abs(s.y-g.y) + abs(s.z -g.z));    }    return 0;}



0 0
原创粉丝点击