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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#include 
#include 
#include 
#include 
#include 
#include 

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






你可能感兴趣的:(dp)