hdu 4597 Play Game 记忆化搜索 区间dp
2015-05-09 22:02
330 查看
Play Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 880 Accepted Submission(s): 514
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
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597
题意:
给你两摞牌,每次可以任意一堆 的牌头或者牌尾抽牌。Alice先抽,Bob后抽,两个人都想抽到最多点数的牌。
做法:
dp[az][ay][bz][by]。 az,ay代表第一堆牌左边 和右边 分别抽到第几张了。然后在这个状态下 Bob抽到的点数。
因为dp表示的Bob的点数,所以牌堆里剩余奇数张牌的时候,是Bob抽,要取各种抽法的最大值。如果只剩偶数张牌,那么是Alice抽,要取 各种抽法中 的最小值。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <stack> #include <queue> #include <vector> #include <deque> #include <set> #include <map> int dp[30][30][30][30]; int a[30]; int b[30]; int dfs(int az,int ay,int bz,int by)//总数是偶数 { if(dp[az][ay][bz][by]!=-1) return dp[az][ay][bz][by]; int sum=ay-az+by-bz+2; int ans; if(sum&1) ans=0; else ans=9999999; if(sum==0) return dp[0][0][0][0]=0; if(az<=ay) { if(sum%2==1)//bob 取 { ans=max(dfs(az+1,ay,bz,by)+a[az],ans); ans=max(dfs(az,ay-1,bz,by)+a[ay],ans); } else { ans=min(dfs(az+1,ay,bz,by),ans); ans=min(dfs(az,ay-1,bz,by),ans); } } if(bz<=by) { if(sum%2==1) { ans=max(dfs(az,ay,bz+1,by)+b[bz],ans); ans=max(dfs(az,ay,bz,by-1)+b[by],ans); } else { ans=min(dfs(az,ay,bz+1,by),ans); ans=min(dfs(az,ay,bz,by-1),ans); } } return dp[az][ay][bz][by]=ans; } int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); memset(dp,-1,sizeof dp); a[0]=a[n+1]=b[0]=b[n+1]=0; int sum=0; for(int i=0;i<n;i++) { scanf("%d",&a[i+1]); sum+=a[i+1]; } for(int i=0;i<n;i++) { scanf("%d",&b[i+1]); sum+=b[i+1]; } printf("%d\n",sum-dfs(1,n,1,n)); } return 0; }
相关文章推荐
- hdu 4597 Play Game (记忆化搜索,区间dp)
- hdu 4597 Play Game (记忆化搜索 区间dp)
- hdu 4597 Play Game(区间dp,记忆化搜索)
- HDU 4597 Play Game (DP,记忆化搜索)
- HDU 4597 Play Game(DFS,区间DP)
- HDU 4597 Play Game (DP,记忆化搜索,博弈)
- HDU 4597 Play Game(DFS,区间DP)
- HDU-4597 Play Game (区间DP)
- hdu 4597 Play Game(区间dp)
- HDU 4597 - Play Game(区间DP)
- hdu 4597 Play Game(区间dp)
- 2013 ACM-ICPC吉林通化全国邀请赛 && HDU 4597 Play Game (博弈 + 区间dp)
- HDU 4597 Play Game (博弈 + 区间dp)
- hdu 4597 Play Game 区间dp
- hdu-4597 Play Game(区间DP)
- HDU 4597 Play Game(区间dp)
- HDU 4597 Play Game (DP,记忆化搜索,博弈)
- HDU-4597-区间DP-(博弈)
- hdu 4597 + uva 10891(一类区间dp)
- HDU 4597 Play Game(记忆化搜索,深搜)