HDU---Delta-wave

Delta-wave

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 19   Accepted Submission(s) : 9

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.

HDU---Delta-wave_第1张图片


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

Statistic | Submit | Back
AC代码:
#include 
#include 
#include 
using namespace std;
int main()
{
    int n,m,sum;
    while(cin>>n>>m)
    {
        int t1,t2,sum;
        t1=sqrt(n);//t1求出有几个横坐标
        if(t1

学习心得:
首先就是找规律, ◇为坐标计算的话步数量为abs(x1-x2)+abs(y1-y2)+abs(i1-i2),把菱形块看成一个坐标点,边缘线分别看成x,y,然后去入手。感谢张鹏大神的讲解~

你可能感兴趣的:(杭电OJ)