HDU 1030 Delta-wave

来源:互联网 发布:电信网通网络转换器 编辑:程序博客网 时间:2024/06/10 17:24


Delta-wave

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


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<cstdio>#include<cmath>using namespace std;int main(){    int m,n,cm,cn,rm,rn,lm,ln;//每一个三角的位置都可以由三个属性描述。c表示level图 ,r表示right图,l表示left图    while(~scanf("%d%d",&m,&n))//    {        cm=(int)ceil(sqrt(m));//C主层次        cn=(int)ceil(sqrt(n));                rm=(m-(cm-1)*(cm-1)-1)/2+1; //R右层次        rn=(n-(cn-1)*(cn-1)-1)/2+1;                lm=(cm*cm-m)/2+1;          //L左层次        ln=(cn*cn-n)/2+1;                int cnt=(int)(fabs(cm-cn)+fabs(lm-ln)+fabs(rm-rn));        printf("%d\n",cnt);    }    return 0;}



原创粉丝点击