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

HDU Delta-wave

时间:2015-05-26 14:26:51      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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

6 12 

Sample Output

3

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

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