hdu4597Play Game(记忆化)

链接

通化邀请赛的题 貌似不怎么难

记忆化DP 状态方程类似于2维的 只是变成了4维  每次有四种方向去搜 取最棒的

判断好边界条件

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stdlib.h>
 6 using namespace std;
 7 int p1[25],p2[25],sum1[25],sum2[25];
 8 int dp[25][25][25][25];
 9 int dfs(int i1,int j1,int i2,int j2)
10 {
11     if(dp[i1][j1][i2][j2]!=-1)
12     return dp[i1][j1][i2][j2];
13     if(i1>j1&&i2>j2)
14     return dp[i1][j1][i2][j2] = 0;
15     int d1=0,d2=0,d3=0,d4=0;
16     int s1 = sum1[j1]-sum1[i1-1];
17     int s2 = sum2[j2]-sum2[i2-1];
18     if(i1<=j1)
19     {
20         d1 = s2+sum1[j1-1]-sum1[i1-1]-dfs(i1,j1-1,i2,j2)+p1[j1];
21         d2 = s2+sum1[j1]-sum1[i1]-dfs(i1+1,j1,i2,j2)+p1[i1];
22     }
23     if(i2<=j2)
24     {
25         d3 = s1+sum2[j2-1]-sum2[i2-1]-dfs(i1,j1,i2,j2-1)+p2[j2];
26         d4 = s1+sum2[j2]-sum2[i2]-dfs(i1,j1,i2+1,j2)+p2[i2];
27     }
28     dp[i1][j1][i2][j2] = max(max(d1,d2),max(d3,d4));
29     return dp[i1][j1][i2][j2];
30 }
31 int main()
32 {
33     int i,n,t;
34     scanf("%d",&t);
35     while(t--)
36     {
37         cin>>n;
38         memset(sum1,0,sizeof(sum1));
39         memset(sum2,0,sizeof(sum2));
40         memset(dp,-1,sizeof(dp));
41         for(i = 1 ; i <= n ;i++)
42         {
43             scanf("%d",&p1[i]);
44             sum1[i] = sum1[i-1]+p1[i];
45         }
46         for(i = 1 ; i<= n ; i++)
47         {
48             scanf("%d",&p2[i]);
49             sum2[i] = sum2[i-1]+p2[i];
50         }
51         int ans = dfs(1,n,1,n);
52         cout<<ans<<endl;
53     }
54     return 0;
55 }
View Code

 

posted @ 2013-08-24 20:48  _雨  阅读(202)  评论(0编辑  收藏  举报