POJ 2385 Apple Catching——记忆搜索或者动态规划

Description

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.

Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).

Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.

Input

  • Line 1: Two space separated integers: T and W

  • Lines 2…T+1: 1 or 2: the tree that will drop an apple each minute.
    Output

  • Line 1: The maximum number of apples Bessie can catch without walking more than W times.

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

Hint

INPUT DETAILS:

Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.

OUTPUT DETAILS:

Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.

题目大意

简单说就是两棵树,FJ一次只能站在一棵树下,最多可以变换w次,问最多能接到多少苹果。

题目解析

思路一

  1. 这种方法会超时,但是思路正确,是后面两种方法的基础。
  2. 不需要额外开辟数组,只需要参数里面有两个或者三个,这里为了好理解,我才用三个参数(其实pos位置完全可以等价于转换次数time的奇偶性得出来,奇数就是在2,偶数就是在1)
  3. 递归终止条件就是当num大于苹果总数时,返回0
  4. 当当前位置接不到苹果时,若变换次数小于w,则看变换位置接到的苹果数和不变换接到的苹果树哪个多;若变换次数大于w,则无能为力,只能继续下一次苹果掉下来。
  5. 当当前位置可以接到苹果,这就不需要变换位置了,直接进行下一次接苹果并加1.
    注意:这种方法超时的原因在于有的solve()函数重复进行运算,导致复杂度增大,所以超时,由此引申第二种解法。

代码:

int solve1(int time,int pos,int num) {
	if(num>t) {
		return 0;
	}
	if(apple[num]!=pos) {
		if(time<=w) {
			return max(solve1(time,pos,num+1),solve1(time+1,pos^1,num+1)+1);
		}
		else{
			return solve1(time,pos,num+1);
		}
	}
	else{
		return solve1(time,pos,num+1)+1;
	}

}

思路二

  1. 这种方法是思路一的改进版,可以AC,原因在于采用了记忆化搜索的方式,减少了计算量和计算时间。将复杂度降低到了O(wt)
  2. 思路和方法一一样,只不过需要开辟额外的数组来保存每次solve计算完的结果,这样就可以在递归过程中,如果当前位置求解过,就可以直接返回,不需要在进行计算了。
  3. 这种思路又是下面一种思路动态规划的前身
    代码:
int solve3(int change,int num){
	if(dp[num][change]>=0){
		return dp[num][change];
	}
	int res=0;
	if(num>t){
		res=0;
	}else if(change<w){
		if(apple[num]==(change%2+1)){
			res=solve3(change,num+1)+1;
		}
		else{
			res=max(solve3(change+1,num+1)+1,solve3(change,num+1));
		}
	}else {
		if(apple[num]==(change%2+1)){
			res=solve3(change,num+1)+1; 
		}
		else{
			res=solve3(change,num+1);
		}
	}
	return dp[num][change]=res;
} 

思路三

  1. 动态规划求解,成功AC。难度要大于前两个方法,但是写法很简单。
  2. 需要初始化数组j=0的位置,所以需要判断第一次下落的苹果的情况。
  3. 如果j=0,那么就是等于前一次接到的苹果,如果当前位置可以接到苹果,那么再加1.
  4. 如果j!=0,先不考虑这次能不能接到苹果,先看之前能接到苹果的最大值,也就是变换次数等于j且前i-1个苹果接到的苹果数dp[i-1][j]和变换次数等于j-1且前i-1个苹果接到的苹果数二者的最大值,然后再看这次能不能接到苹果,能的话+1.

代码:

int solve2(){
	int dp[1001][31];
	memset(dp,0,sizeof(dp));
	if(apple[1]==1){
		dp[1][0]=1;
		dp[1][1]=0;
	}
	else{
		dp[1][0]=0;
		dp[1][1]=1;
	}
	for(int i=2;i<=t;i++){
		for(int j=0;j<=w;j++){
			if(j==0){
				dp[i][j]=dp[i-1][j];
				if(apple[i]==1){
					dp[i][j]++;
				}
			}else{
				dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
				if(apple[i]==(j%2+1)){
					dp[i][j]++;
				}
			}
		}
	}
	return dp[t][w];
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值