J - Delta-wave;
杭电oj1030题;看图
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<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int a, b, sum;
int h1,h2,t1,t2,tt1,tt2,wz1,wz2,wy1,wy2;
while(scanf("%d %d",&a, &b) != EOF){
h1 = (int)sqrt(a);
if(h1*h1!=a){
h1=h1+1;
}
h2 = (int)sqrt(b);
if(h2*h2!=b){
h2=h2+1;
}
t1=h1*h1;
t2=h2*h2;
wz1=abs(a-t1)/2+1;
wz2=abs(b-t2)/2+1;
tt1=(h1-1)*(h1-1)+1;
tt2=(h2-1)*(h2-1)+1;
wy1=abs(a-tt1)/2+1;
wy2=abs(b-tt2)/2+1;
sum = abs(wy1-wy2)+abs(wz1-wz2)+abs(h1-h2);
printf("%d\n",sum);
}
return 0 ;
}
题目意思就是如图(hdu1030题)排列数字,输入两个数字,输出他们间的最短路径也就是从这个数字空间到那个数字表记的空间经过的线;
如图,我们可以把这些线划分为三类,
一个是水平线
一个是向左的线
一个是向右的线
在想要如何计算;
水平线就是他们之间相差几行就是会有几个行线;这就要知道他们分别是那几行;
就是把a进行sqrt;如果结果是小数则要进行加一;
`
h1 = (int)sqrt(a);
if(h1*h1!=a){
h1=h1+1;
}
向右和向左其实是类似的;
就是按图片那种说法;找到各自的行;去相减最后得到的就是各自的线的个数
//