版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33096883/article/details/77484579
题目地址
http://poj.org/problem?id=1661
题目大意
- 场景中包括多个长度和高度各不相同的平台。地面是最低的平台,高度为零,长度无限。
- Jimmy老鼠在时刻0从高于所有平台的某处开始下落,
它的下落速度始终为1米/秒。当Jimmy落到某个平台上
时,游戏者选择让它向左还是向右跑,它跑动的速度
也是1米/秒。当Jimmy跑到平台的边缘时,开始继续下
落。 - Jimmy每次下落的高度不能超过MAX米,不然就
会摔死,游戏也会结束。 - 设计一个程序,计算Jimmy到地面时可能的最早时间。
解题思路
- Jimmy每到一个平台,都有二种选择,左和右
- 不断的递归,直到地面
Code
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#define INF 0x3fffffff
#define N 1100
using namespace std;
typedef long long LL;
int n, x, y, MAX;
struct Node {
int l, r, h;
bool operator < (const Node b) const {
return h > b.h;
}
};
Node node[N];
int left_time[N];
int right_time[N];
int min_time(int cur, int is_left) {
int high = node[cur].h;
int target;
if (is_left) {
target = node[cur].l;
} else {
target = node[cur].r;
}
int next;
for (next = cur+1; next <= n; next++) {
if (node[next].l <= target && target <= node[next].r) {
break;
}
}
if (next <= n) {
if (high - node[next].h > MAX) {
return INF;
}
} else {
if (high > MAX) {
return INF;
} else {
return high;
}
}
int l_time = high - node[next].h + target - node[next].l;
int r_time = high - node[next].h + node[next].r - target;
if (left_time[next] == -1) {
left_time[next] = min_time(next, true);
}
if (right_time[next] == -1) {
right_time[next] = min_time(next, false);
}
l_time += left_time[next];
r_time += right_time[next];
return min(l_time, r_time);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#else
//
#endif
int t;
cin >> t;
while (t--) {
memset(left_time, -1, sizeof(left_time));
memset(right_time, -1, sizeof(right_time));
cin >> n >> x >> y >> MAX;
node[0].l = x;
node[0].r = x;
node[0].h = y;
for (int i = 1; i <= n; i++) {
cin >> node[i].l >> node[i].r >> node[i].h;
}
sort(node, node+n+1);
printf("%d\n", min_time(0, true));
}
return 0;
}