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

Ural Collegiate Programming Contest 1998

找规律题:

我们把上下两个相连的三角形构成的菱形看做一个格,左斜线看做横坐标,右斜线看做纵坐标,也是四个方向(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)那么就肯定是最短路径了。

时间: 2024-08-07 23:37:09

hdu 1030 Delta-wave的相关文章

HDU 3723 Delta Wave(默慈金数)

传送门 Delta Wave Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1160    Accepted Submission(s): 370 Problem Description A delta wave is a high amplitude brain wave in humans with a frequency of 1

LA 5092 && hdu 3723 Delta Wave (卡特兰数)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3723 and http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20568 题意:有种折线每向右延伸一个单位长度,高度要么不变,要么加1,要么减1.而且任何时刻高度不能低于0.求这种折线最终高度为0的情况总数. 分析:由于任何时刻斜向上的线不小于斜向下的线的数目,而且最终相等,,,,,卡特兰数模型.卡特兰数资料 若有i条斜向上的线,那

HDU 3723 Delta Wave (高精度+calelan数)

题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答案应该是sum(C(n, 2*i)*C(n, i)/(i+1)) = a1 + a2 + a3 + ....,然后可以再化简一下,成为一个递推式ai = ai-1 * (n-2*i+1)*(n-2*i+2)/(k*(k+1)): 这次用记事本写的,然后没有测试,直接交的,虽然CE了一发,但还是挺好的

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

UVA - 1478 Delta Wave (大数+卡特兰数)

Description A delta wave is a high amplitude brain wave in humans with a frequency of 1 - 4 hertz which can be recorded with an electroencephalogram (EEG) and is usually associated with slow-wave sleep (SWS). - from Wikipedia The researchers have dis

hdu 1030

Delta-wave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5611    Accepted Submission(s): 2137 Problem Description A triangle field is numbered with successive integers in the way shown on the

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 ed

UVa 1478 - Delta Wave

题目:求从(0,0)到(N,0)的路径数,每次可以斜向上或者斜向下或者直走.(不能走到负区域) 分析:组合,计数,卡塔兰数,大整数. 因为不能走到负的区域,所以上升的和下降的此时必然相等,而且上升次数要随时不小于下降次数. 由此可知,上升和下降是上面提到的括号合法匹配,枚举所有的上升下降次数有: 边长为n的图中走法数 F(n)= Σ(C(n,2*i)*Ci),其中: C(n,2*i)是n条路中有i条上升和i条下降的方案数,Ci是i条上升和i条下降的合法组合数(内部). 化简:F(n)= Σ(C(

HDU 1030 Delta-wave 数学题解

给出一个数字塔,然后求沿着数字之间的边走,给出两个数字,问其路径最短的长度是多少. 看似一条搜索题目,只是有一定做题经验的人都知道,这个不是搜索题,直接搜索肯定超时. 这个是依据规律计算的数学题目. 我这里的思路是一层一层往下搜,利用层间的规律加速,实现层跃,到了同一层,或者同一个对角列的时候就能够直接计算出结果了.对角列即顺着三角形的边能直接走到目标的列. 数学计算出层与层之间相差2,而也能够利用这个规律计算N和M所在的层和列. 这样做由点麻烦,只是我自己琢磨出来的,不错的思路.O(∩_∩)O