POJ 1925 Spiderman (DP) __ Dynamic programming

Source: Internet
Author: User
Description

Dr. Octopus kidnapped Spiderman ' s girlfriend M.j. And kept her in the West Tower. Now the hero, Spiderman, has-to-reach the tower as soon as him can to rescue her, using his own weapon, the Web.

From Spiderman's apartment, where he starts, to the tower there is a straight road. Alongside of the road stand many tall buildings, which are definitely or taller to his equal. Spiderman can shoot his web to the top of any building between the tower and himself (including the tower), and then swing To the other side of the building. At the "moment he finishes" swing, he can shoot him web to another building and make another swing until him gets to the West Tower. Figure-1 shows how Spiderman gets to the "tower" from "the Top", "Apartment–he" from "A to B", from B to C, and swings M C to the tower. All of the buildings (including the tower) are treated as straight lines, and during his swings he can ' t hit the ground, whic h means the length of the web is shorter or equal to the height of the building. Notice that during Spiderman ' s swings and he can never go backwards.

You may assume this each swing takes a unit of time. As in Figure-1, Spiderman used 3 swings to reach the tower, and your can easily find out this there is no better way.


Input

The "a" of the input contains the number of test Cases K (1 <= k <= 20). Each case starts with a line containing a single integer n (2 <= n <= 5000), the number of buildings (including the Apartment and the tower). N lines follow and each line contains two integers xi., Yi, (0 <= xi., Yi <= 1000000) The position and height of the B Uilding. The always the apartment and the last one is always the tower. The input is sorted by Xi value in ascending order and no two buildings have the same X value.


Output

For each test case, output one line containing the minimum number of swings (if it's possible to reach the tower) or-1 if Spiderman can ' t reach the tower.


Sample Input

2
6
0 3
3 5
4 3 5 5
7 4 4 3 0 3 3 4 10 4


Sample Output

3
-1


the

Given n buildings, each building is represented by a two-integer x,y, x represents its position on the horizontal axis, y represents its height, all the height of the building is greater than the height of the first building, and the input order of the building is arranged according to X from small to large.

Spider-Man is on the first building, he's going to the last building to save his girlfriend, every time Spider-Man can swing to the symmetrical position of the building, and the length of the silk thread can not be larger than the height of the building, to find the minimum swing number of the last building.


train of Thought

Dp[i] Represents the minimum number of swings required for his position to the X axis I, then the state transition equation: dp[i]=min (dp[i],dp[j]+1)

For each I, enumerate behind it and can reach the position of I J.


AC Code

 #include <iostream> #include <cstdio> #include <cstring> #include <
Cmath> using namespace std;

#define INF 0x3f3f3f3f typedef __int64 LL;
int dp[1000010];
LL x[5010],y[5010];

int n;
    void Solve () {memset (dp,inf,sizeof (DP));
    dp[x[0]]=0;  for (int i=1; i<n; i++) {int l=sqrt (y[i]*y[i]-(y[i]-y[0)) * (y[i]-y[0));
            Calculate the farthest distance for the construction I to accept for (int j=max (X[0],X[I]-L); j<x[i]; J + +) {if (dp[j]==inf) continue;
            int k=min (X[N-1],2*X[I]-J);
        Dp[k]=min (dp[k],dp[j]+1);
} printf ("%d\n", dp[x[n-1]]==inf?-1:dp[x[n-1]);
    int main () {int T;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        for (int i=0; i<n; i++) scanf ("%i64d%i64d", x+i,y+i);
    Solve ();
return 0; }

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.