首页 > 学院 > 开发设计 > 正文

poj 1661

2019-11-11 05:04:01
字体:
来源:转载
供稿:网友

“Help Jimmy” 是在下图所示的场景上完成的游戏

场景中包括多个长度和高度各不相同的平台。地面是最低的平台,高度为零,长度无限。

Jimmy老鼠在时刻0从高于所有平台的某处开始下落,它的下落速度始终为1米/秒。当Jimmy落到某个平台上时,游戏者选择让它向左还是向右跑,它跑动的速度也是1米/秒。当Jimmy跑到平台的边缘时,开始继续下落。Jimmy每次下落的高度不能超过MAX米,不然就会摔死,游戏也会结束。

设计一个程序,计算Jimmy到底地面时可能的最早时间。 Input 第一行是测试数据的组数t(0 <= t <= 20)。每组测试数据的第一行是四个整数N,X,Y,MAX,用空格分隔。N是平台的数目(不包括地面),X和Y是Jimmy开始下落的位置的横竖坐标,MAX是一次下落的最大高度。接下来的N行每行描述一个平台,包括三个整数,X1[i],X2[i]和H[i]。H[i]表示平台的高度,X1[i]和X2[i]表示平台左右端点的横坐标。1 <= N <= 1000,-20000 <= X, X1[i], X2[i] <= 20000,0 < H[i] < Y <= 20000(i = 1..N)。所有坐标的单位都是米。

Jimmy的大小和平台的厚度均忽略不计。如果Jimmy恰好落在某个平台的边缘,被视为落在平台上。所有的平台均不重叠或相连。测试数据保证问题一定有解。 Output 对输入的每组测试数据,输出一个整数,Jimmy到底地面时可能的最早时间。 Sample Input 1 3 8 17 20 0 10 8 0 10 13 4 14 3 Sample Output 23

按照高度排序,在加上地面和最高层。从下往上,每次都是从这块板的左右边界求得上一块板的左右边界的最短时间。

#include <cstdio>#include <iostream>#include <cmath>#include <algorithm>using namespace std;struct Platform{ int x1,x2,high;};const int MAXN = 1010;#define INF 9000000int N, X, Y, MAX; Platform plat[MAXN]; int dp[MAXN][2]; int cmp(Platform a,Platform b){ return a.high<b.high;}void LeftMinTime(int i){ int k=i-1; while(k>0&&plat[i].high-plat[k].high<=MAX) { if(plat[i].x1>=plat[k].x1&&plat[i].x1<=plat[k].x2) { dp[i][0]=plat[i].high-plat[k].high+ min(dp[k][0]+plat[i].x1-plat[k].x1,dp[k][1]+plat[k].x2-plat[i].x1); return ; } else --k; } if(plat[i].high-plat[k].high>MAX) dp[i][0]=INF; else dp[i][0]=plat[i].high;}void RightMinTime(int i){ int k=i-1; while(k>0&&plat[i].high-plat[k].high<=MAX) { if(plat[k].x1-plat[i].x2<=0&&plat[i].x2-plat[k].x2<=0) { dp[i][1]=plat[i].high-plat[k].high+min(dp[k][0]+plat[i].x2-plat[k].x1,dp[k][1]+plat[k].x2-plat[i].x2); return ; } else k--; } if(plat[i].high-plat[k].high>MAX) { dp[i][1]=INF; } else dp[i][1]=plat[i].high;}int ShortestTime(){ int i,j; for(i=1;i<=N+1;i++) { LeftMinTime(i); RightMinTime(i); } return min(dp[N+1][0],dp[N+1][1]);}int main(){ int t,i; while(scanf("%d",&t)!=EOF) { while(t--!=0) { scanf("%d%d%d%d",&N,&X,&Y,&MAX); for(i=1;i<=N;i++) { scanf("%d%d%d",&plat[i].x1,&plat[i].x2,&plat[i].high); } plat[0].high=0; plat[0].x1=-20000; plat[0].x2=20000; plat[N+1].high=Y; plat[N+1].x1=X; plat[N+1].x2=X; sort(plat,plat+N+2,cmp); PRintf("%d/n",ShortestTime() ); } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表