• hdu 1030 Delta-wave


    Delta-wave

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


    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
     
     
    找规律题:
    我们把上下两个相连的三角形构成的菱形看做一个格,左斜线看做横坐标,右斜线看做纵坐标,也是四个方向(0,-1),(0,1),(-1,0),(1,0)变化,那么就相当于直角坐标了。
    
    从(x1,y1)到(x2,y2),这是斜角坐标,距离dis=abs(x1-x2)+abs(y1-y2);我们还需要考虑菱形中的移动,这个都是一层移动一次。
    
    所以答案ans=dis+abs(a-b)=abs(x1-x2)+abs(y1-y2)+abs(a-b),其中a和b表示各自层数,x1,y1,x2,y2表示两个数所在菱形在斜角坐标系的位置。
    利用这个思路写程序就很简单的了。

    不过为什么这样会正确?好像还是很难解析清楚,我理解这是一个规律,跨过了两个不同的斜边,就会得到最短的路径了,因为本题一定要走边,不能走顶点跨到下一个三角形的,故此上下左右的边的最小相隔边数,就是题目要求的最短距离数了。也就可以想象为跨过三角形的三个边,三个方向的边,刚好是上下的h高度边,左斜线的x边,右斜线的y边,(有人也喜欢用x,y,z)那么就肯定是最短路径了。

  • 相关阅读:
    启动或重启Oracle数据以及监听
    占坑 对自己的目标要求
    线程的使用经验(包括 Thread/Executor/Lock-free/阻塞/并发/锁等)
    接口的调用Client测试
    解决ftp登录问题:500 OOPS: cannot change directory:/home/xxx 500 OOPS: child died
    Tomcat7性能调优
    mysql性能调优
    四:JVM调优原理与常见异常处理方案
    redis的线程模型 与 压力测试
    为什么要用消息队列 及 自己如何设计一个mq架构
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5926432.html
Copyright © 2020-2023  润新知