Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8011 | Accepted: 2549 |
Description
Input
Output
Sample Input
1 3 8 17 20 0 10 8 0 10 13 4 14 3
Sample Output
23
纠结了一天了,终于AC了,因为从起点直接下落到地面的情况没有考虑,所以一直WA,开始一直想不出哪里不对,泪崩啊 T_T~~~~。
#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> using namespace std; #define MAX_VAL 999999999 typedef struct { int height; int left; int right; }Board; bool cmp(const Board &b1, const Board &b2) { return b1.height > b2.height; } int main() { //dp[i][0]代表走到第i个木板左端的最少时间,dp[i][1]代表走到第i个木板右端的最少时间 int dp[1005][2]; Board board[1005]; int nCase, x, y, MAX, n, ans; scanf("%d", &nCase); while(nCase--) { memset(dp, 0, sizeof(dp)); ans = MAX_VAL; scanf("%d%d%d%d", &n, &x, &y, &MAX); for (int i = 1; i <= n; i++) { scanf("%d%d%d", &board[i].left, &board[i].right, &board[i].height); dp[i][0] = dp[i][1] = MAX_VAL; } board[0].height = y; //把起点当成长度为0高度为y的平台,因为有可能直接从起点下落到地面 board[0].left = board[0].right = x; //按照高度从高到低排序 sort(board, board + n + 1, cmp); board[n + 1].height = 0; board[n + 1].left = -20000; board[n + 1].right = 20000; dp[0][0] = 0; dp[0][1] = 0; for (int i = 0; i <= n; i++) { //bleft和bright用于标记从第i块木板左右两端下落是否找到了下一个落脚点 bool bleft = true, bright = true; for (int j = i + 1; j <= n + 1; j++) { //如果两端都找到了落脚点或者下落的距离大于MAX则退出循环 if (!bleft && !bright || board[i].height - board[j].height > MAX) { break; } if (board[j].left <= board[i].left && board[j].right >= board[i].left && board[i].height != board[j].height && bleft) { bleft = false; if (j == n + 1) { //如果到了地面,则直接记录下最小时间 ans = min(ans, dp[i][0]); } else { //在这里dp[j][0]表示从第i块木板左端下落,走到第j块左端的最小时间 dp[j][0] = min(board[i].left - board[j].left + dp[i][0], dp[j][0]); //在这里dp[j][1]表示从第i块木板左端下落,走到第j块右端的最小时间 dp[j][1] = min(board[j].right - board[i].left + dp[i][0], dp[j][1]); } } if (board[j].left <= board[i].right && board[j].right >= board[i].right && board[i].height != board[j].height && bright) { bright = false; if (j == n + 1) { ans = min(ans, dp[i][1]); } else { //在这里dp[j][0]表示从第i块右端木板下落,走到第j块左端的最小时间 dp[j][1] = min(board[j].right - board[i].right + dp[i][1], dp[j][1]); //在这里dp[j][0]表示从第i块右端木板下落,走到第j块左端的最小时间 dp[j][0] = min(board[i].right - board[j].left + dp[i][1], dp[j][0]); } } } } printf("%d\n", ans + y);//总距离为横向加上纵向的距离,纵向的总距离则为y } return 0; }