HDU-1030,Delta-wave

简介: HDU-1030,Delta-wave

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


解题思路:


就是一个找规律的题, 用三个坐标(行 左列 右列 例如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]=(abs(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]=(abs(m-b[1]*b[1])+2)/2;
    ans=abs(a[1]-b[1])+abs(a[2]-b[2])+abs(a[3]-b[3]);
    cout<<ans<<endl;
  }
  return 0;
}


相关文章
|
Java
poj 1205 :Water Treatment Plants (DP+高精度)
思路:DP+高精度。DP部分,易得最右边城市的状态只可能用3种:>V, V, <。故分三种状态讨论,设dp[i][0]为第i个城市的状态为:> V ,dp[i][1]为:V ,dp[i][2]为:<。由实际流动的可能性可以得到状态转移方程:
38 0
|
机器人
HDU-1035,Robot Motion(DFS)
HDU-1035,Robot Motion(DFS)
Google Earth Engine ——MODIS Terra/Aqua Daily BAI烧伤面积指数(BAI)
Google Earth Engine ——MODIS Terra/Aqua Daily BAI烧伤面积指数(BAI)
129 0
Google Earth Engine ——MODIS Terra/Aqua Daily BAI烧伤面积指数(BAI)
POJ 2840 Big Clock
POJ 2840 Big Clock
111 0
|
消息中间件 数据建模
题解 P1339 【[USACO09OCT]热浪Heat Wave】
题目链接 这道题纯属是一个裸的SPFA;建议先把模板AC之后再做。只需要做一些手脚,就是在加边的时候加一个双向边就好。然后再第一次加点的时候看不懂模板的出门左转度娘。推荐下面一片讲解:友链所以说,直接上代码。
1140 0