搜索题
因为是两个人选数,并且都希望得到最优,随意需要枚举搜索4中情况,从中找到最优的
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LLEN 30
int card1[LLEN], card2[LLEN];
int dp[LLEN][LLEN][LLEN][LLEN];
int sum1[LLEN], sum2[LLEN];
int max(int a, int b) {
return a > b ? a : b;
}
int dfs(int top1, int below1, int top2, int below2) {
if(dp[top1][below1][top2][below2]) {
return dp[top1][below1][top2][below2];
}
if(top1 < below1 && top2 < below2) {
dp[top1][below1][top2][below2] = 0;
return 0;
}
int sum = 0;
int ans = 0;
if(top1 >= below1) sum += sum1[top1] - sum1[below1-1];
if(top2 >= below2) sum += sum2[top2] - sum2[below2-1];
if(top1 >= below1) {
ans = max(ans, sum - dfs(top1,below1+1,top2,below2));
ans = max(ans, sum - dfs(top1-1,below1,top2,below2));
}
if(top2 >= below2) {
ans = max(ans, sum - dfs(top1,below1,top2,below2+1));
ans = max(ans, sum - dfs(top1,below1,top2-1,below2));
}
dp[top1][below1][top2][below2] = ans;
return ans;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n;
memset(dp, 0, sizeof(dp));
memset(sum1, 0, sizeof(sum1));
memset(sum2, 0, sizeof(sum2));
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &card1[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%d", &card2[i]);
}
sum1[0] = sum2[0] = 0;
for(int i = 1; i <= n; i++) {
sum1[i] = sum1[i-1] + card1[i];
}
for(int i = 1; i <= n; i++) {
sum2[i] = sum2[i-1] + card2[i];
}
printf("%d\n", dfs(n,1,n,1));
}
return 0;
}