HDU2073 Infinite Road "water problem" "recursion"

Source: Internet
Author: User

The Infinite Road

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 6113 Accepted S Ubmission (s): 3097
Problem Description
Sweet since childhood like to draw pictures, recently he bought a smart brush, because just contact, so sweet will only use it to draw a straight line, so he in the plane Cartesian coordinate system to draw the following graphic:
Sweet friend Honey found the above figure is still a bit of a rule, so he asked the sweet: in your drawing, I give you two points, please calculate a connection two points of the length of the line (that is, the length of the route along the line) bar.

Input
The first number is a positive integer n (≤100). The number of groups representing the data.
Each set of data consists of four non-negative integers x1,y1,x2,y2; all numbers are not greater than 100.

Output
For each set of data, the line distance between two points (X1,y1) and (x2,y2) is output. Note that the output is accurate to 3 digits after the decimal point.

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

Author
Lily

Source

Zhejiang University of Technology Network tryouts


The main topic: as shown in the figure, give you two coordinates, to find two points between the line distance.

Idea: Using recursion. Here, the process of recursion from (0,0) point to (x, y) point polyline is simulated.

Set I as horizontal axis and J as ordinate. The path is first right down to the vertical axis of 0, then one step to the horizontal

Coordinates are 0, and the ordinate is the position of the original horizontal axis +1.

so the direct simulation of recursive derivation. Use Ans[x][y] to save (0,0) The distance from the point to (x, y) points. Ultimately

The result is: Fabs (Ans[x1][y1]-ans[x2][y2]).


#include <iostream> #include <algorithm> #include <cstdio> #include <cmath>using namespace std; Double Ans[220][220];int Main () {for    (int j = 1; J <; J + +)    {for        (int i = 0; I <= J; i++)        {            if (i = = 0)                ANS[I][J] = Ans[j-1][i] + sqrt (1.0*j*j+1.0* (j-1) * (j-1));            else                ans[i][j-i] = ans[i-1][j-i+1] + sqrt (2.0);        }    }    int N;    int x1,y1,x2,y2;    Cin >> N;    while (n--)    {        cin >> x1 >> y1 >> x2 >> y2;        printf ("%.3lf\n", Fabs (Ans[x1][y1]-ans[x2][y2]));    }    return 0;}


HDU2073 Infinite Road "water problem" "recursion"

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.