话说,写的好差劲,好乱,中间老是写错。、思路就是从上往下模拟 T_ T, 貌似算法很挫。
对于每块板设置属性,x1(左端点),x2(右端点), h(高度), t1(从起点到达左端点 的最短时间), t2(从起点到右端点的最短时间),将起点视为一块左右端点相同的板,地面视为左右端点最大的板。每次从上往下更新。
//9204859 ylwh 1661 Accepted 404K 16MS G++ 2721B 2011-08-21 13:38:26
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define MAX 200000000
struct floor
{
int t1, t2, x1, x2, h;
}f[1001];
int cmp(struct floor a, struct floor b)
{
return a.h > b.h;
}
int main(void)
{
int n, maxh, i, j, k, t, cnt, temp, flag1, flag2;
scanf("%d", &t);
while(t--)
{
cnt = 0;
scanf("%d%d%d%d", &n, &f[0].x1, &f[0].h, &maxh);
f[0].x2 = f[0].x1;
f[0].t1 = f[0].t2 = 0;
for(i=1; i<=n; i++)
{
scanf("%d%d%d", &f[i].x1, &f[i].x2, &f[i].h);
f[i].t1 = f[i].t2 = MAX;
}
f[n+1].x1 = -20000;
f[n+1].x2 = 20000;
f[n+1].t1 = f[n+1].t2 = MAX;
f[n+1].h = 0;
sort(f, f+n+2, cmp);
for(i=0; i<n+1; i++)
{
flag1 = flag2 = 1;
for(j=i+1; (flag1 || flag2) && j<n+2 && f[i].h - f[j].h <= maxh; j++)//继续的条件是f[i]左右端点可以继续更新,且f[i],与f[j]之间的高度差不大于maxh
{
if(f[j].h >= f[i].h)
continue;
if(flag1 && j == n+1 )
{
flag1 = 0;
if( f[j].t1 > f[i].t1 + f[i].h - f[j].h )
f[j].t1 = f[i].t1 + f[i].h -f[j].h;
}
if(flag2 && j == n+1)
{
flag2 = 0;
if(f[j].t1 > f[i].t2 + f[i].h - f[j].h)
f[j].t1 = f[i].t2 + f[i].h -f[j].h;
}
if(flag1 && f[i].x1 >= f[j].x1 && f[i].x1 <= f[j].x2 && f[i].t1 != MAX)
{
temp = f[i].h - f[j].h + f[i].x1 - f[j].x1;
if(f[j].t1 > f[i].t1 + temp)
f[j].t1 = f[i].t1 + temp;
temp = f[i].h - f[j].h + f[j].x2 - f[i].x1;
if(f[j].t2 > f[i].t1 + temp)
f[j].t2 = f[i].t1 + temp;
}
if(f[i].x1 < f[j].x2 && f[i].x1 > f[j].x1)//这里判断端点一是否能继续往下更新
flag1 = 0;
if(flag2 && f[i].x2 >= f[j].x1 && f[i].x2 <= f[j].x2 && f[i].t2 != MAX)
{
temp = f[i].h - f[j].h + f[i].x2 - f[j].x1;
if(f[j].t1 > f[i].t2 + temp)
f[j].t1 = f[i].t2 + temp;
temp = f[i].h - f[j].h + f[j].x2 - f[i].x2;
if(f[j].t2 > f[i].t2 + temp)
f[j].t2 = f[i].t2 + temp;
}
if(f[i].x2 < f[j].x2 && f[i].x2 > f[j].x1)
flag2 = 0;
}
}
printf("%d\n", f[n+1].t1);
}
return 0;
}