标签:
Delta-wave
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 6
Font: Times New Roman | Verdana |
Georgia
Font Size: ← →
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
Sample Output
Source
这是一个三角形每层所容纳的数加2,且第i层共容纳i*i各数。求第n个数到第m个数需要几步(只能通过2个三角形的公共边穿过)。
不难发现从第i1层穿到第i2层最少需要abs(i1-i2)步,经过大量的数据统计发现以◇为坐标计算的话步数量为abs(x1-x2)+abs(y1-y2)+abs(i1-i2)
ps:这道题是我这次作业最后做的代码一次ac感觉真棒!
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
int n,m;
while(cin>>n>>m){
int t1,t2,i;
t1=sqrt(n);
if(t1<sqrt(n))t1++;
t2=sqrt(m);
if(t2<sqrt(m))t2++;
if(n==1)t1=1;
if(m==1)t2=1;
int x1=n,x2=m,y1=n,y2=m;
x1-=(t1-1)*(t1-1);
x2-=(t2-1)*(t2-1);
y1=x1;
y2=x2;
if(x1%2!=0)x1++;
x1/=2;
if(x2%2!=0)x2++;
x2/=2;
y2/=2;
y2++;
y1/=2;
y1++;
y2=t2-y2;
y1=t1-y1;
int sum=0;
cout<<(sum=abs(x1-x2)+abs(t1-t2)+abs(y1-y2))<<'\12';
}
return 0;
}
运行结果:
HDU Delta-wave
标签:
原文地址:http://blog.csdn.net/zp___waj/article/details/46006635