~~~
# Delta-wave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6931 Accepted Submission(s): 2674
~~~
Problem Description
A triangle field is numbered with successive integers in the way shown on the picture below.
![](https://box.kancloud.cn/2016-02-29_56d3edfe0ed4c.jpg)
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
~~~
**求最近的路径的大小,本质上这个题中每一个块可以表示为三个坐标的形式,那就是水平方向的第几层,左侧的左侧第几列,右侧看右侧第几列,比如说:1这个元素在水平第1层,左侧第1列,右侧第1列; 6这个元素是水平第3层,左侧第2层,右侧第1层,那么1到6的计算就是这三个坐标的差的绝对值之和:2+1+0 = 3 ,也就是说1到6需要3步。**
剩下的自然是怎么求出来各个方向上的位置咯:水平方向是level = sqrt(num - 1) + 1,左侧的坐标可以表示为:left = ( level*level - num)/ 2 +1,
右侧的坐标可以表示为:right = (num- (level-1)*(level-1) - 1)/ 2 + 1
那么剩下的就是将两个点的三个坐标的值对应求差然后去绝对值再求和就可以了
~~~
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext())
{
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int level1 = (int)Math.sqrt(num1 - 1) + 1;
int left4num1 = (level1*level1 - num1)/2 + 1;
int right4num1 = (num1 - (level1 - 1) * (level1 - 1) - 1)/2 + 1;
int level2 = (int)Math.sqrt(num2 - 1) + 1;
int left4num2 = (level2*level2 - num2)/2 + 1;
int right4num2 = (num2 - (level2 - 1) * (level2 - 1) - 1)/2 + 1;
System.out.println(Math.abs(level1 - level2) + Math.abs(left4num1 - left4num2) + Math.abs(right4num1 - right4num2));
}
}
}
~~~
- 前言
- 求和的问题ACM
- A+B问题acm
- 1091ACM求和
- 杭电ACM1092求和问题详解
- ACM杭电的1093求和问题
- 杭电ACM1094计算A+B的问题
- 杭电ACM1095解决A+B问题
- 杭电ACM1096求和问题
- 杭电Acm1001解决求和的问题
- 杭电ACM1008电梯问题C++
- 杭电ACM大赛2000关于ASCII码排序的问题
- 杭电ACM2006奇数的乘积
- 杭电ACM数值统计2008
- 杭电ACM1019求最大公约数
- 杭电ACM1108求最小公倍数
- 杭电ACM2035人见人爱的A^B
- 杭电ACM1061N^N求最右边的数的问题
- 杭电ACM1021裴波纳挈数AGAIN
- 杭电ACm1005求f(n)非递归
- 杭电ACM1071The area---------求积分面积
- 杭电ACM吃糖果问题
- 杭电ACm求数列的和2009
- 杭电ACM多项式求和--》2011
- 杭电ACM。。。sort
- 杭电ACM1004
- 杭电ACM2043密码的问题已经AC
- 杭电ACM2041楼梯问题
- 动态规划C++::杭电ACM1003
- 杭电ACM----2018母牛的故事
- 杭电ACM2007平方和与立方和
- 卢卡斯队列
- 全国软件2. 三人年龄
- 全国软件3. 考察团组成
- 全国软件--微生物增殖
- 全国软件填写算式
- 全国软件-----------猜生日
- 全国软件---------欧拉与鸡蛋
- Java经典算法四十例编程详解+程序实例
- 杭电ACMA + B Problem II问题解析
- 杭电ACM1018BigNumber解析
- 杭电ACM1088 Write a simple HTML Browser Java
- 杭电ACM1106排序Java代码
- 杭电ACM 1012 u Calculate e java
- 杭电ACM 1020 Encoding java解析
- 杭电1047 An Easy Task - java 解读
- 杭电ACM 1040 As Easy As A+B java 解读
- 杭电ACM 1041 Computer Transformation java代码详解AC
- 杭电ACM 1030 Delta-wave java代码解析