1.26 [hduoj] 2073 endless path

Problem Description

Sweet habit of drawing pictures, he recently bought a Smart Brush, just because of the contact, so sweet and will only use it to draw a straight line, so he had to draw the following graphics plane Cartesian coordinate system:
 



Sweet honey honey found a good friend or a little above chart rules, so he asked sweetly: In your drawings, I give you two points, you calculate the length of the fold line connecting two points (ie along polyline walk the line length) it.

Input

The first number is the positive integer N (≤100). The number of sets of data representative.
Each set of data consists of four non-negative integers x1, y1, x2, y2; all numbers will not be greater than 100.

Output

For each test, the fold line distance between the output points (x1, y1), (x2, y2). Note that the output three decimal place.

Sample Input

5

0 0 0 1

0 0 1 0

2 3 3 1

99 99 9 9

5 5 5 5

Sample Output

1.000

2.414

10.646

54985.047

0.000

#include<stdio.h>
#include<math.h>
#define G2 sqrt(2)
double result;
int xx1,xx2,yy1,yy2,s1,s2,n;
double p2(int a,int b)
{
    double part2=0;
    for(int i=a;i<b;i++)
        part2+=sqrt(i*i+(i+1)*(i+1));
    return part2;
}
double p1(int a,int b)
{
    double part1=0;
    for(int i=a+1;i<b;i++)
        part1+=G2*i;
    return part1;
}
void main()
{
    while(scanf("%d",&n)!=EOF)
    {
        while(n--)
        {
            int flag=0;
            scanf("%d%d%d%d",&xx1,&yy1,&xx2,&yy2);
            s1=xx1+yy1;
            s2=xx2+yy2;
            if(s1>s2)
            {
                int temp;
                temp=s1;
                s1=s2;
                s2=temp;
                flag=1;
            }
            if(s2-s1==0)
                result=sqrt((xx1-xx2)*(xx1-xx2)+(yy1-yy2)*(yy1-yy2));
            if(s2-s1==1)
            {
                if(flag==0)
                    result=sqrt((xx1-s1)*(xx1-s1)+yy1*yy1)+sqrt(s1*s1+s2*s2)+sqrt(xx2*xx2+(yy2-s2)*(yy2-s2));
                if(flag==1)
                    result=sqrt((xx2-s1)*(xx2-s1)+yy2*yy2)+sqrt(s1*s1+s2*s2)+sqrt(xx1*xx1+(yy1-s2)*(yy1-s2));
            }
            if(s2-s1!=0&&s2-s1!=1)
            {
                if(flag==0)
                    result=sqrt((xx1-s1)*(xx1-s1)+yy1*yy1)+sqrt(xx2*xx2+(yy2-s2)*(yy2-s2))+p1(s1,s2)+p2(s1,s2);
                if(flag==1)
                    result=sqrt((xx2-s1)*(xx2-s1)+yy2*yy2)+sqrt(xx1*xx1+(yy1-s2)*(yy1-s2))+p1(s1,s2)+p2(s1,s2);
            }
            printf("%.3lf\n",result);
        }
    }
}

This question I feel bad start, too slowly pondering the law.

First, I found that the function, points on the same line, with the abscissa and ordinate of the same.

I then x + y = n where the line function is referred to as "decent line" from x + y = n-1 transitions to x + y = n of the line is called "spare wire"

We want to count the lines into three parts, the first part is removed decent two points where the lines, the second line part are all amateurs, the last part of the line is a decent two points are located.

The first two points abscissa and ordinate addition, if the difference is equal to 0 the addition, the direct distance between the two calculated points like; if the sum is equal to a difference, even if the second and third section like. If greater than 1, it should be considered in three parts.

 

Published 153 original articles · won praise 4 · Views 3697

Guess you like

Origin blog.csdn.net/qq_39782006/article/details/104079370