码迷,mamicode.com
首页 > 其他好文 > 详细

杭电 1030 Delta-wave

时间:2014-05-11 19:56:01      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

Delta-wave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5447    Accepted Submission(s): 2063


Problem Description
A triangle field is numbered with successive integers in the way shown on the picture below.

bubuko.com,布布扣


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


一个找规律的题, 用三个坐标(行 左列 右列 例如12的坐标为423)把每个数的位置找出来,最短路程就是各个坐标差的绝对值的和。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
	int ans,a[4],b[4],n,m;
	while(cin>>n>>m)
	{
		a[1]=sqrt(n-1)+1;//行坐标
		a[2]=(n-(a[1]-1)*(a[1]-1)+1)/2;//左列坐标
		a[3]=(fabs(n-a[1]*a[1])+2)/2;//右列坐标
		b[1]=sqrt(m-1)+1;
		b[2]=(m-(b[1]-1)*(b[1]-1)+1)/2;
		b[3]=(fabs(m-b[1]*b[1])+2)/2;
		/*cout<<a[1]<<a[2]<<a[3]<<endl;
		cout<<b[1]<<b[2]<<b[3]<<endl;
*/
		ans=fabs(a[1]-b[1])+fabs(a[2]-b[2])+fabs(a[3]-b[3]);
		cout<<ans<<endl;
	}

	return 0;
}


杭电 1030 Delta-wave,布布扣,bubuko.com

杭电 1030 Delta-wave

标签:des   style   blog   class   code   java   

原文地址:http://blog.csdn.net/fanerxiaoqinnian/article/details/25540967

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!