HDU 4597 Play Game (记忆化搜索)无语的节奏

来源:互联网 发布:阿尔法淘宝宝贝 编辑:程序博客网 时间:2024/06/15 01:59

Play Game

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


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两人取牌,取牌只能取a、b两堆中的其中一堆的头或尾,A先取,(A、B取最优的情况)求A最大的分数。
AC代码:

#include <cstdio>#include <cstring>#include <iostream>using namespace std;#define max(a,b) a>b?a:b; int mp[3][25],dp[25][25][25][25];int dfs(int l1,int r1,int l2,int r2,int sum){int m=0;if(l1>r1&&l2>r2)return 0;if(dp[l1][r1][l2][r2])return dp[l1][r1][l2][r2];if(l1<=r1){m=max(m,sum-dfs(l1+1,r1,l2,r2,sum-mp[0][l1]));//轮到先手则为总的减后手m=max(m,sum-dfs(l1,r1-1,l2,r2,sum-mp[0][r1]));//轮到后手则为当时的总和减去先手}//相当于总的-后手=先手(先手分部加起来)if(l2<=r2){m=max(m,sum-dfs(l1,r1,l2+1,r2,sum-mp[1][l2]));m=max(m,sum-dfs(l1,r1,l2,r2-1,sum-mp[1][r2]));}dp[l1][r1][l2][r2]=m;return m;}int main(){int n,t,i,j,sum;scanf("%d",&t);while(t--){sum=0;memset(mp,0,sizeof(mp));memset(dp,0,sizeof(dp));scanf("%d",&n);for(i=0;i<2;i++){for(j=1;j<=n;j++){//注意j要从1开始,如果从0开始就会越界,造成RE!!!scanf("%d",&mp[i][j]);sum+=mp[i][j];}}printf("%d\n",dfs(1,n,1,n,sum));}return 0;}

0 0
原创粉丝点击