hdu 4597 Play Game 记忆化搜索 区间dp

Play Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 880    Accepted Submission(s): 514


Problem Description
Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his total score. Alice and Bob are both clever enough, and will pick up cards to get as many scores as possible. Do you know how many scores can Alice get if he picks up first?
 

Input
The first line contains an integer T (T≤100), indicating the number of cases. 
Each case contains 3 lines. The first line is the N (N≤20). The second line contains N integer a i (1≤a i≤10000). The third line contains N integer b i (1≤b i≤10000).
 

Output
For each case, output an integer, indicating the most score Alice can get.
 

Sample Input
  
  
2 1 23 53 3 10 100 20 2 4 3
 

Sample Output
  
  
53 105


链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597

题意:

给你两摞牌,每次可以任意一堆 的牌头或者牌尾抽牌。Alice先抽,Bob后抽,两个人都想抽到最多点数的牌。



做法:

dp[az][ay][bz][by]。 az,ay代表第一堆牌左边 和右边 分别抽到第几张了。然后在这个状态下 Bob抽到的点数。

因为dp表示的Bob的点数,所以牌堆里剩余奇数张牌的时候,是Bob抽,要取各种抽法的最大值。如果只剩偶数张牌,那么是Alice抽,要取 各种抽法中 的最小值。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>

int dp[30][30][30][30];
int a[30];
int b[30];
int dfs(int az,int ay,int bz,int by)//总数是偶数
{
	if(dp[az][ay][bz][by]!=-1)
		return dp[az][ay][bz][by];
	
	int sum=ay-az+by-bz+2;
	int ans;
	if(sum&1)
		ans=0;
	else
		ans=9999999;
	if(sum==0)
		return dp[0][0][0][0]=0;
	if(az<=ay)
	{
		if(sum%2==1)//bob 取 
		{
			ans=max(dfs(az+1,ay,bz,by)+a[az],ans); 
			ans=max(dfs(az,ay-1,bz,by)+a[ay],ans); 
		}
		else
		{
			ans=min(dfs(az+1,ay,bz,by),ans); 
			ans=min(dfs(az,ay-1,bz,by),ans); 
		}
	}
	if(bz<=by)
	{
		if(sum%2==1)
		{
			ans=max(dfs(az,ay,bz+1,by)+b[bz],ans); 
			ans=max(dfs(az,ay,bz,by-1)+b[by],ans);  
		}
		else
		{
			ans=min(dfs(az,ay,bz+1,by),ans);
			ans=min(dfs(az,ay,bz,by-1),ans);  
		}
	}
	return dp[az][ay][bz][by]=ans;
}


int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		memset(dp,-1,sizeof dp);
		a[0]=a[n+1]=b[0]=b[n+1]=0;
		int sum=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i+1]);
			sum+=a[i+1];
		}
		for(int i=0;i<n;i++)
		{
			scanf("%d",&b[i+1]);
			sum+=b[i+1];
		}
		printf("%d\n",sum-dfs(1,n,1,n));
	}
	return 0;
}
 






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值