Delta-wave
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5273 Accepted Submission(s): 1986
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
主要是想辦法求出三個坐標!
AC代碼如下:
#include
#include
int cs(double x)
{
return (int )sqrt(x-0.5)+1;
}
int main()
{
int s,f[100000],j,i,c1,d1,c2,d2,c,d;
f[1]=1;
for(j=2;j<100000;j++)
f[j]=f[j-1]+(2*cs(f[j-1])-1);
double a,b,t;
while(~scanf("%lf%lf",&a,&b))
{
if(a>b)
{
t=a;
a=b;
b=t;
}
c1=cs(a);
d1=(int )a-f[c1]+1;
c2=cs(b);
d2=(int )b-f[c2]+1;
//printf("%d %d \n%d %d\n",c1,d1,c2,d2);
c=fabs(c2-c1);
s=c+fabs((d2-1)/2-(d1-1)/2)+fabs(c-(d2/2-d1/2));
printf("%d\n",s);
}
return 0;
}