| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 378 人关注过本帖
标题:大家帮忙看看,太谢谢了
取消只看楼主 加入收藏
枫叶无痕
Rank: 2
等 级:论坛游民
帖 子:80
专家分:30
注 册:2011-2-10
结帖率:73.91%
收藏
已结贴  问题点数:20 回复次数:0 
大家帮忙看看,太谢谢了
Delta-wave

Time Limit:1000MS  Memory Limit:65536K
Total Submit:178 Accepted:66

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

Ural Collegiate Programming Contest 1998


#include<stdio.h>
main()
{
    long n,m;
    long i,j,k,e,f,d,s,h;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        e=f=1;
        while((n<e*e-2*e+2)||(n>e*e))
    {
        e++;
    }
        while((m<f*f-2*f+2)||(m>f*f))
    {
        f++;
    }
    if(e==f)
    {
        d=m-n;
        printf("%ld\n",d);
    }
    else
    {                                                                  
            if((n-(e*e-2*e+2)+1)%2==0)
            {
                s=f*f-2*f+2+n-(e*e-2*e+2)-1;
                h=s+2*(f-e+1);
                if(m>=s&&m<=h)
                {
                    if((m-(f*f-2*f+2)+1)%2==1)
                    {
                        d=2*(f-e)+1;
                        printf("%ld\n",d);
                    }
                    else
                    {
                        d=2*(f-e);
                        printf("%ld\n",d);
                    }
                }
                else
                {
                    if(m>h)
                    {
                        d=2*(f-e)+1+(m-h);
                        printf("%ld\n",d);
                    }
                    if(m<s)
                    {
                        d=2*(f-e)+1+(s-m);
                        printf("%ld\n",d);
                    }
                }
            }
            else
            {
                s=f*f-2*f+2+n-(e*e-2*e+2)-1;
                h=s+2*(f-e);
                if(m>=s&&m<=h)
                {
                    if((m-(f*f-2*f+1))%2==1)
                    {
                        d=2*(f-e);
                        printf("%ld\n",d);
                    }
                    else
                    {
                        d=2*(f-e)-1;
                        printf("%ld\n",d);
                    }
                }
                else
                {
                    if(m>h)
                    {
                        d=2*(f-e)+(m-h)-1;
                        printf("%ld\n",d);
                    }
                    if(m<s)
                    {
                        d=2*(f-e)+(s-m)-1;
                        printf("%ld\n",d);
                    }
                }
        }
    }
    }
}


大家帮忙看看
搜索更多相关主题的帖子: cell 
2011-04-13 16:58
快速回复:大家帮忙看看,太谢谢了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018787 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved