题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597
数据这么小,要么三方以上的一个DP,或者就是暴力搜索
这个题目记忆化搜索吧!
两个人是等价的,每个人都想要得到自己在某一状态的最大分数,那么就就用dp数组来记录每个状态得到的最优值
搜索的时候明显的是一个dfs的过程,从上面进去,得出结果是从低上来的,所以说遇到dp不为0的肯定就是本状态的最优解了!
sum 当前剩下的
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
#define maxn 30
#define MAX(a,b) (a>b?a:b)
int dp[maxn][maxn][maxn][maxn];
int a[maxn],b[maxn];
int n;
int find_ans(int f1,int f2,int f3,int f4,int sum){
int Max=0;
if(f2 < f1 && f4 < f3) return 0;
if(dp[f1][f2][f3][f4] > 0) return dp[f1][f2][f3][f4];
if(f1 <= f2){
Max=MAX(sum-find_ans(f1+1,f2,f3,f4,sum-a[f1]),Max);
Max=MAX(sum-find_ans(f1,f2-1,f3,f4,sum-a[f2]),Max);
}
if(f3 <= f4){
Max=MAX(sum-find_ans(f1,f2,f3+1,f4,sum-b[f3]),Max);
Max=MAX(sum-find_ans(f1,f2,f3,f4-1,sum-b[f4]),Max);
}
dp[f1][f2][f3][f4]=Max;
return Max;
}
int main(){
int i,j,k,t,sum;
scanf("%d",&t);
while(t--){
sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
for(i=1;i<=n;i++){
scanf("%d",&b[i]);
sum+=b[i];
}
memset(dp,0,sizeof(dp));
printf("%d\n",find_ans(1,n,1,n,sum));
}
return 0;
}
本文提供了一道HDU ACM题库中编号4597的题目解决方案,采用记忆化搜索和动态规划的方法求解两人游戏中的最大得分策略。代码实现了四维DP数组用于记录各种状态下的最优解。
499

被折叠的 条评论
为什么被折叠?



