HDUOJ----(1030)Delta-wave

Delta-wave

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

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

Ural Collegiate Programming Contest 1998

比如6 16 向右移一格..经过两条边即可...然后将其分层,确定他们的行列来计算相应的值即可。。哎呀,说不清了..

来看代码ba!。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 using namespace std;
 7 int sum(int m,int &cc)
 8 {
 9  
10   int rr=(int)sqrt(1.0*m);  //确定她所在第几行..一右边为参数  
11     if(rr*rr!=m)    rr++; 
12      cc=rr*rr-m;
13     if(cc&1)
14       return 2*(rr-1);
15     else 
16         return 2*rr-1;
17 }
18 
19 int main()
20 {
21    int n,m,ncc,mcc,add;
22  while(cin>>m>>n)
23    {
24        int rankn=sum(n,ncc);   //得到n的所在第几行
25        int rankm=sum(m,mcc);   //得到m的所在第几行
26            ncc>>=1;            //得到n所在第几列,以右边为参数
27            mcc>>=1;            //得到m所在第几列,以右边为参数
28    //此处可以进一步优化...现在就不优化了..
29            if(rankn==rankm)
30            cout<<abs(n-m)<<endl;
31        else 
32            if(rankn<rankm)  
33            {
34            
35               if(mcc>=ncc&&mcc<=(rankm-rankn)/2+ncc)
36                       cout<<(rankm-rankn)<<endl;
37               else
38               { 
39                 
40                  if(2*mcc<(rankm-rankn)/2+2*ncc)
41                 {
42                      add=ncc-mcc;
43                      cout<<2*add+(rankm-rankn)<<endl;
44                  }
45                   else
46                   {
47                    add=mcc-((rankm-rankn)/2+ncc);
48                    cout<<2*add+(rankm-rankn)<<endl;
49                   }
50 
51               }
52            }
53         else
54         {
55             //rankn>rankm
56             if(ncc>=mcc&&ncc<=(rankn-rankm)/2+mcc)
57              cout<<(rankn-rankm)<<endl;
58            else
59            {
60                if(2*ncc<(rankn-rankm)/2+2*mcc)
61                { 
62                add=mcc-ncc;
63                cout<<2*add+(rankn-rankm)<<endl;
64                }
65                else
66                {
67                  add=ncc-((rankn-rankm)/2+mcc);
68                  cout<<2*add+rankn-rankm<<endl;
69                }
70            }
71         }
72    /* cout<<0&1<<endl;*/
73 }
74    
75    return 0;
76 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏杨熹的专栏

【LEETCODE】模拟面试-134-Gas Station

新生 题目: https://leetcode.com/problems/gas-station/ There are N gas stations alon...

35560
来自专栏小樱的经验随笔

HDU 1000 A + B Problem(指针版)

A + B Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276...

28340
来自专栏算法修养

HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144...

409100
来自专栏小樱的经验随笔

HDU 2502 月之数(二进制,规律)

月之数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/...

34740
来自专栏java、Spring、技术分享

深入分析Spring MVC中RequestBody与ResponseBody

  在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成请求报文到对象和对象到响应报文的转换。在Sprin...

31310
来自专栏算法修养

HDU 5157 Harry and magic string(回文树)

Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

37580
来自专栏wym

Educational Codeforces Round 44 (Rated for Div. 2)A. Chess Placing

You are given a chessboard of size 1 × n. It is guaranteed that n is even. The c...

13120
来自专栏前端儿

VF

Vasya is the beginning mathematician. He decided to make an important contributi...

9710
来自专栏ml

HDUOJ 2672---god is a girl 《斐波那契数》

god is a girl Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3276...

28960
来自专栏ml

HDUOJ---The Moving Points

The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/...

33540

扫码关注云+社区

领取腾讯云代金券