# HDU 4597（记忆化搜索 dfs 参考）

830人阅读 评论(0)

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 

Source

PS:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 32;
int a[maxn], b[maxn], suma[maxn], sumb[maxn];
int dp[maxn][maxn][maxn][maxn];
//dp[x][y][i][j]表示当前玩家从a堆的x~y,b堆的i~j能获得的最大价值
int dfs(int x, int y, int i, int j)
{
if(dp[x][y][i][j])
{
return dp[x][y][i][j];
}
if(x > y && i > j)
{
return 0;
}
int maxa = 0, maxb = 0;
if(x <= y)
{
maxa = max(a[x]+dfs(x+1,y,i,j),a[y]+dfs(x,y-1,i,j));
}
if(i <= j)
{
maxb = max(b[i]+dfs(x,y,i+1,j),b[j]+dfs(x,y,i,j-1));
}
dp[x][y][i][j] = suma[y]-suma[x-1]+sumb[j]-sumb[i-1]-max(maxa,maxb);

//区间和减去对手所取的剩下的就为当前玩家的
return dp[x][y][i][j];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
memset(suma, 0,sizeof(suma));
memset(sumb, 0,sizeof(sumb));
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
suma[i] = suma[i-1]+a[i];
}
for(int i = 1; i <= n; i++)
{
scanf("%d",&b[i]);
sumb[i] = sumb[i-1]+b[i];
}
int ans = suma[n]+sumb[n]-dfs(1,n,1,n);
printf("%d\n",ans);
}
return 0;
}


1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
Motto
• 你必须非常努力，
• 才能看起来毫不费力。
• tianyiming：2013/10/15 18:00
• wdnlwzd：2014/07/30 21:32
• vasttian： 2016/12/29 12:00
Vasttian
Blogs
个人资料
• 访问：1010985次
• 积分：20363
• 等级：
• 排名：第449名
• 原创：869篇
• 转载：72篇
• 译文：0篇
• 评论：247条
阅读排行