Spiderman
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5858 Accepted: 1143

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. 

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

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;

LL x[5555],h[5555];
int  n;
LL dp[2000002];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%I64d%I64d",x+i,h+i);
        memset(dp,63,sizeof(dp));
        dp[x[1]]=0;
        for(int i=2;i<=n;i++)
        {
            for(int j=x-1;j>=x[1];j--)
            {
                LL d=(j-x)*(j-x)+(h-h[1])*(h-h[1]);
                if(d>h*h) break;
                dp[2*x-j]=min(dp[2*x-j],dp[j]+1);
            }
        }
        LL ans=0x3f3f3f3f;
        for(int i=x[n];i<=2*x[n];i++)
            ans=min(ans,dp);
        if(ans>=0x3f3f3f3f)
            ans=-1;
        printf("%d\n",ans);
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 1925 Spiderman的更多相关文章

  1. POJ 1925 Spiderman(DP)

    题目链接 这个破题,好不容易思路清楚了,写的就是过不了..关键部分直接抄的别人的...终于A了,自己写的判断什么的,就是有一组数据过不了. #include <cstdio> #inclu ...

  2. [POJ 2397] Spiderman

    Link: POJ 2397 传送门 Solution: 设$dp[i][j]$表示第$i$步走到$j$高度时经过的最高高度 分向上走和向下走两种方式转移即可 注意记录路径,最后输出时要逆序输出 (逆 ...

  3. poj很好很有层次感(转)

    OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...

  4. POJ题目分类推荐 (很好很有层次感)

    著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...

  5. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  6. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  7. POJ数据的输入输出格式

    POJ在评阅习题时需要向程序提供输入数据,并获取程序的输出结果.因此提交的程序需按照每个习题具体的输入输出格式要求处理输入输出.有的时候,测评系统给出程序的评判结果是“数据错误”或“结果错误”,有可能 ...

  8. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  9. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

随机推荐

  1. Zabbix邮件报警-->Script

    Version:3.0.1 邮件报警有两种media 1.Email zabbix发送报警邮件到指定smtp服务器(使用系统自带的sendmail,发送邮箱是zabbix服务器的本地邮箱账号) 再由s ...

  2. UVa 437 The Tower of Babylon(经典动态规划)

    传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...

  3. HDU 1231 最大连续子序列(水题)

    题目链接: 传送门 最大连续子序列 Time Limit: 1000MS     Memory Limit: 32768 K Description 给定K个整数的序列{ N1, N2, ..., N ...

  4. 用DOS命令配置服务开机自启动

    2016-08-19 15:01 Create 使用命令  sc  config 参考博客:http://blog.csdn.net/it1988888/article/details/7992626 ...

  5. hdu 2042 - 不容易系列之二

    题意:(略)太长了 解法:找规律,发现A[i]=A[i-1]*2-2; 1: #include<stdlib.h> 2: #include<string.h> 3: #incl ...

  6. 数据结构与算法分析 - 网络流入门(Network Flow)

    转载:网络流基础篇--Edmond-Karp算法             BY纳米黑客 网络流的相关定义: 源点:有n个点,有m条有向边,有一个点很特殊,只出不进,叫做源点. 汇点:另一个点也很特殊, ...

  7. Linux 中 17 个 tar 命令实用示例

    Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...

  8. centos7 + VMware Workstation Pro

    centos7 + VMware Workstation Pro安装 centos 7 镜像文件 下载地址https://www.centos.org/download/ 笔者是使用的DVD ISO, ...

  9. wpf 保存控件中的内容为图片格式

    黄色的是wpf控件的名称! //保存到特定路径            FileStream fs = new FileStream(@"C:\image.png", FileMod ...

  10. SQL Server编程(05)游标【转载】

    在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: 而对于游标来说: ...