poj 1661 help Jimmy

把资料上的解释摘下来了:

 

n 此题目的“子问题”是什么呢?
n Jimmy 跳到一块板上后,可以有两种选择,向左走或向右走。走到左端和走到右端所需的时间,容易算出。
n 如果我们能知道,以左端为起点到达地面的最短时间,和以右端为起点到达地面的最短时间,那么向左走还是向右走,就很容选择了。
n 因此,整个问题就被分解成两个子问题,即Jimmy 所在位置下方第一块板左端为起点到地面的最短时间,和右端为起点到地面的最短时间。这两个子问题在形式上和原问题是完全一致的。
n
代码在编译错误了N次,WA了一次后终于AC。
编译错误是因为资料用了好多C不允许但C++允许的格式。。。崩溃。长记性了。

代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <string.h>

using namespace std;
const int MAX = 0x3f3f3f3f;
const int maxn = 1000 + 10;

struct node
{
	int lp, rp, height;
}stage[maxn];

int stage_num;
int limit;				//最大下降高度
int dp[maxn][2];			//dp[i][j] 从第i块板子 向j方向走时的最短时间
int casenum;
int sx, sy;				//开始位置的坐标

int cmp(const void * a, const void * b)				//根据平台 高度排序
{
	node *_a = (node *)a;
	node *_b = (node *)b;

	return  -(_a->height) + (_b->height);
}



int work(int level,int y)				//计算 在第x块 平台上 向y方向走 可以调入地面的最短时间 y = 0 左方向
{
	if (-1 != dp[level][y])
		return dp[level][y];
	
	int cur_height = stage[level].height;
	int next_level, next_x;
	if (0 == y)
		next_x = stage[level].lp;
	else
		next_x = stage[level].rp;


	for (next_level = level + 1; next_level <= stage_num; next_level++)
	{
		if (stage[next_level].lp <= next_x && next_x <= stage[next_level].rp)
			break;
	}

	if (next_level > stage_num)			//直接落在地面上
	{
		if (stage[level].height > limit)
			return MAX;
		else
			return stage[level].height;
	}
	int airtime;			//降落时间
	airtime = stage[level].height - stage[next_level].height;
	if (airtime > limit)
		return MAX;

	int ltime = 0, rtime = 0;		//落在 next_level 块板子上向左或向右 走的时间
	ltime = airtime + next_x - stage[next_level].lp;
	rtime = airtime + stage[next_level].rp - next_x;

	ltime += work(next_level, 0);
	rtime += work(next_level, 1);
	dp[level][y] =  ltime < rtime ? ltime : rtime;
	return dp[level][y];
}


int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	cin >> casenum;
	while (casenum--)
	{
		memset(stage, 0, sizeof(stage));
		memset(dp, -1, sizeof(dp));
		
		cin >> stage_num >> sx >> sy >> limit;
		for (int i = 1; i <= stage_num; i++)
		{
			cin >> stage[i].lp >> stage[i].rp >> stage[i].height;
		}
		stage[0].lp = sx;
		stage[0].rp = sx;
		stage[0].height = sy;

		qsort(stage, stage_num  +1, sizeof(node), cmp);
		cout << work(0, 1) << endl;
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值