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. 帝国cms制作手机网站

    1.操作前,我们需要先对网站数据库进行备份. 接下来我们添加手机站的模板组.点击"模板", 选择"模板组管理"中的"导入/导出模板组",然后 ...

  2. 数据结构作业——sights(最短路/最近公共祖先)

    sights Description 美丽的小风姑娘打算去旅游散心,她走进了一座山,发现这座山有 n 个景点,由于山路难修,所以施工队只修了最少条的路,来保证 n 个景点联通,娇弱的小风姑娘不想走那么 ...

  3. Ubuntu学习总结-02 Ubuntu下的FTP服务的安装和设置

    一 安装vsftpd 在安装前vsftpd,先更新apt-get下载的数据源输入如下命令: sudo apt-get update 然后安装vsftpd sudo apt-get install vs ...

  4. HDU5672String(尺标法)

    问题描述 有一个 10\leq10≤长度\leq 1,000,000≤1,000,000 的字符串,仅由小写字母构成.求有多少个子串,包含有至少k(1 \leq k \leq 26)k(1≤k≤26) ...

  5. POJ1050To the Max(求最大子矩阵)

    题目链接 题意:给出N*N的矩阵,求一个子矩阵使得子矩阵中元素和最大 分析: 必备知识:求一组数的最大连续和 int a[N]; ,maxn = -INF; ; i <= n; i++) { i ...

  6. spring--学习之IOC DI

    2.1.1  IoC是什么 Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器 ...

  7. Navicat 的使用(一)

    1.创建连接 主机名 : 可以不写名称随意 主机名/IP地址:localhost或者127.0.0.1 都是本机的意思 端口:默认3306   尽量不要改怕与其余端口重复,如有重名端口系统会报错 用户 ...

  8. 最近在 OS-10.9下配置opencv, cgal, latex, qt, pillow

    其实我之前使用的Mac os的版本是10.8的雪豹,可是最近想体验一下Mac os10.9新版本,于是就开始更新Mac os,经过10多个小时的下载和成功安装后,发现之前的配置全乱了,首先是发现lat ...

  9. JavaWeb学习笔记——javabean

  10. ecshop 变量表

    get_goods_info($goods_id) 商品详情 get_sales_count($goods_id)  商品销量 get_promote_goods()   参与促销商品  Mobile