码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1925

时间:2014-09-03 18:15:07      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   ar   for   

Spiderman
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6232   Accepted: 1221

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 he 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 taller or equal to his apartment. 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 the swing, he can shoot his web to another building and make another swing until he gets to the west tower. Figure-1 shows how Spiderman gets to the tower from the top of his apartment – he swings from A to B, from B to C, and from C to the tower. All the buildings (including the tower) are treated as straight lines, and during his swings he can‘t hit the ground, which means the length of the web is shorter or equal to the height of the building. Notice that during Spiderman‘s swings, he can never go backwards. 
bubuko.com,布布扣

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

Input

The first line 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 building. The first building is 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
10 4
3
0 3
3 4
10 4

Sample Output

3
-1

Source

Beijing 2004 Preliminary@POJ

AC代码:

#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
int x[5005],y[5005];
ll h[5005];
int f[2000005];
int main(){
    int T; scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&x[i],&y[i]);
            h[i]=y[i]*y[i]-(y[i]-y[1])*(y[i]-y[1]);
        }
        for(int i=x[1];i<=2*x[n];i++)
            f[i]=-1;
        f[x[1]]=0;
        for(int i=2;i<=n;i++){
            for(int j=x[i]-1;j>=x[1];j--){
                if(f[j]==-1)
                    continue;
                if((x[i]-j)*(x[i]-j)>h[i])
                    break;
                int tt=2*x[i]-j;
                if(f[tt]==-1 || f[tt]>f[j]+1)
                    f[tt]=f[j]+1;
                if(tt>x[n] && (f[x[n]]==-1 || f[x[n]]>f[tt]))
                    f[x[n]]=f[tt];
            }
        }
        printf("%d\n",f[x[n]]);
    }
    return 0;
}


poj 1925

标签:des   style   http   color   os   io   strong   ar   for   

原文地址:http://blog.csdn.net/my_acm/article/details/39029909

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!