HDU 1030 Delta-wave

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1030

Delta-wave

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 6821 Accepted Submission (s): 2628

Problem Descriptiona triangle field is numbered with successive integers in the the-the-the-the-picture shown.  The traveller needs to go from the cell with number M to the cell with number N. The traveller is able-enter the cell through cell edges only, he canCellTo 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.

Inputinput contains the 1 to 1000000000 separated with space (s), numbers M and N in the range.

Outputoutput should contain the length of the shortest route.

Sample INPUT6 12

Sample OUTPUT3

To find a regular problem, the position of each triangle is represented by three-dimensional coordinates (x, y, z), x for the horizontal layer from top to bottom, y for left to right, and Z for right to left

The nearest distance from the nth triangle to the M-triangle = the sum of the various coordinate differences

i.e.: D = ABS (X1-X2) + ABS (Y1-Y2) + ABS (Z1-Z2);

The nth triangle illustrates a total of n triangles from 1 to N, judging by the number of lines that it is the coordinate value X

Use for loop to find

for (i = 1;; i + = 2)//i represents the number of triangles in a row, the number of triangles in each line is 2, so I add 2 each time
{
if (n-i <= 0)//If the number of triangles at this time is less than or equal to the number of triangles in a row, then the coordinates can already be determined, at which point N represents the number of the nth triangle in the X row
{
y = (i-n)/2 + 1;
Z = (n + 1)/2;
Break
}//find coordinates y and Z
x++;//if n is greater than the number of triangles in the previous I row, the number of rows where n is the next line, i.e. X + +
n-= i;
}

Combine diagrams to understand

Http://images.cppblog.com/cppblog_com/guodongshan/1030.jpg

#include <stdio.h>#include<math.h>#include<stdlib.h>#include<algorithm>using namespacestd;intMain () {intN, I, M, x1, y1, Z1, x2, Y2, Z2, D;  while(~SCANF ("%d%d", &n, &m)) {x1= x2 =1;  for(i =1; ; i + =2)        {            if(N-i <=0) {y1= (i-n)/2+1; Z1= (n +1) /2;  Break; } x1++; N-=i; }         for(i =1; ; i + =2)        {            if(M-i <=0) {y2= (i-m)/2+1; Z2= (M +1) /2;  Break; } X2++; M-=i; } d= ABS (X1-X2) + ABS (Y1-Y2) + ABS (z1-z2); printf ("%d\n", D); }    return 0;}

HDU 1030 Delta-wave

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.