play c语言,C语言记忆化搜索___Play Game(Hdu 4597)

Play Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 822    Accepted Submission(s): 474

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 ai (1≤ai≤10000). The third line contains N integer bi (1≤bi≤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

题意:有两堆卡片,每堆有相同数量的卡片,且每张卡片上面有数字,现在A,B轮流从这两堆卡片中每次抽取一张卡片(每次抽取只能从顶部或者底部抽取).要求将所有卡片抽取完后,每人所抽取卡片上的数字要求尽可能大,假设AB两人足够聪明,问A最后的数字总和是多少.每组测试数据有三行,第一行为每堆卡片的数目,第二行和第三行分别是两对卡片上对应的数字,输出A最后的数字总和.

分析:

这题应该算是区间dp吧,可以看一下这题的原型:

其他规则都一样,但是只有一个数字序列,也是每次只能拿左右两端的一个数字,问最终Alice拿多少?

只有一行数字序列可以用f(i, j)表示数字序列还剩下区间[i,j]段时开始拿,最多可以拿多少数字

而这题只是变成了两行数字序列, 那么可以在上面的基础上,再增加两维

变成f(i, j, k, l), 表示第一个序列剩下区间[i,j],第二个序列剩下区间[k,l]的情况下开始拿,最多可以拿多少?

当面临状态f(i, j, k, l) 时,你有四种选择:

1. 选择第一行的最左边数字

2. 选择第一行的最右边数字

3. 选择第二行的最左边数字

4. 选择第二行的最右边数字

所以, f(i, j, k, l)可以由:

f(i+1, j, k, l)

f(i, j-1, k, l)

f(i, j, k+1, l)

f(i, j, k, l-1)

这四种状态转移而来,

假设当前状态是Alice要选择,那么上一个状态就是Bob选择的最大值,

为了要让Alice的最终和最大,那么就要选择上面四种状态最小的一个转,

设sum(i, j, k, l) 表示地一个序列[i,j]段之和与第二个序列的[k,l]段之和的和。

sum(i, j, k, l)  - 上一次Bob拿的值就等于Alice能拿到的值

f(i, j, k, l) = sum(i, j, k, l) -min{  f(i+1, j, k, l), f(i, j-1, k, l), f(i, j, k+1, l), f(i, j, k, l-1) };

上代码:

#include

#include

int dp[30][30][30][30];

int arr1[30],arr2[30],sum1[30],sum2[30];

int max(int a,int b)

{

if(a>b) return a;

return b;

}

int dfs(int l1,int r1,int l2,int r2)

{

if(dp[l1][r1][l2][r2]!=-1)return dp[l1][r1][l2][r2];

if(l1>r1&&l2>r2) return dp[l1][r1][l2][r2]=0;

int ans=0;

int sum=0;

if(l1<=r1) sum+=sum1[r1]-sum1[l1-1];

if(l2<=r2) sum+=sum2[r2]-sum2[l2-1];

if(l1<=r1)

{

ans=max(ans,sum-dfs(l1+1,r1,l2,r2));

ans=max(ans,sum-dfs(l1,r1-1,l2,r2));

}

if(l2<=r2)

{

ans=max(ans,sum-dfs(l1,r1,l2+1,r2));

ans=max(ans,sum-dfs(l1,r1,l2,r2-1));

}

return dp[l1][r1][l2][r2]=ans;

}

int main()

{

int T,i,n;

scanf("%d",&T);

while(T--)

{

scanf("%d",&n);

for(i=1;i<=n;i++)

{

scanf("%d",&arr1[i]);

sum1[i]=sum1[i-1]+arr1[i];

}

for(i=1;i<=n;i++)

{

scanf("%d",&arr2[i]);

sum2[i]=sum2[i-1]+arr2[i];

}

memset(dp,-1,sizeof(dp));

printf("%d\n",dfs(1,n,1,n));

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值