HDU - 4597 Play Game (博弈dp)
Posted xuejye
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 4597 Play Game (博弈dp)相关的知识,希望对你有一定的参考价值。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597
Play Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1927 Accepted Submission(s): 1145
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
很简单的一道题 就是经典博弈区间dp取的层数变成了两层而已
但是因为不是写成递归 两层之间互相有影响 使得判边界的时候写的很糟糕 和自己原本边界写在外面的习惯不一样 记录发。。
#include<bits/stdc++.h>
using namespace std;
int a[111];
int b[111];
int dp[22][22][22][22];
int suma[111];
int sumb[111];
int main()
int t;
scanf("%d",&t);
while(t--)
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
memset(dp,0,sizeof(dp));
memset(suma,0,sizeof(suma));
memset(sumb,0,sizeof(sumb));
for(int i=1;i<=n;i++)
suma[i]=suma[i-1]+a[i];
sumb[i]=sumb[i-1]+b[i];
for(int lb=0;lb<=n;lb++)
for(int ib=1;ib+lb-1<=n;ib++)
int jb=ib+lb-1;
for(int la=0;la<=n;la++)
for(int ia=1;ia+la-1<=n;ia++)
int ja=ia+la-1;
if(lb)
dp[ia][ja][ib][jb]=max(dp[ia][ja][ib][jb],suma[ja]-suma[ia-1]+sumb[jb-1]-sumb[ib-1]-dp[ia][ja][ib][jb-1]+b[jb]);
dp[ia][ja][ib][jb]=max(dp[ia][ja][ib][jb],suma[ja]-suma[ia-1]+sumb[jb]-sumb[ib]-dp[ia][ja][ib+1][jb]+b[ib]);
if(la)
dp[ia][ja][ib][jb]=max(dp[ia][ja][ib][jb],suma[ja-1]-suma[ia-1]+sumb[jb]-sumb[ib-1]-dp[ia][ja-1][ib][jb]+a[ja]);
dp[ia][ja][ib][jb]=max(dp[ia][ja][ib][jb],suma[ja]-suma[ia]+sumb[jb]-sumb[ib-1]-dp[ia+1][ja][ib][jb]+a[ia]);
//cout <<ia << " " << ja << " " << ib << " " << jb << " " <<dp[ia][ja][ib][jb] <<endl;
printf("%d\\n",dp[1][n][1][n]);
以上是关于HDU - 4597 Play Game (博弈dp)的主要内容,如果未能解决你的问题,请参考以下文章