hdu 4597(记忆化搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597

思路:要保证当前状态下,得到的是最优的,dfs+dp.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int Left[44],Right[44];
 8 int dp[22][22][22][22];
 9 int n,ans,MAX;
10 
11 int dfs(int upl,int downl,int upr,int downr,int sum)
12 {
13     if(dp[upl][downl][upr][downr])return dp[upl][downl][upr][downr];
14     int ans=0;
15     if(upl<=downl){
16         ans=max(ans,sum-dfs(upl+1,downl,upr,downr,sum-Left[upl]));
17         ans=max(ans,sum-dfs(upl,downl-1,upr,downr,sum-Left[downl]));
18     }
19     if(upr<=downr){
20         ans=max(ans,sum-dfs(upl,downl,upr+1,downr,sum-Right[upr]));
21         ans=max(ans,sum-dfs(upl,downl,upr,downr-1,sum-Right[downr]));
22     }
23     return dp[upl][downl][upr][downr]=ans;
24 }
25 
26 int main()
27 {
28   //  freopen("1.txt","r",stdin);
29     int _case,sum;
30     scanf("%d",&_case);
31     while(_case--){
32         scanf("%d",&n);
33         for(int i=1;i<=n;i++)scanf("%d",&Left[i]),sum+=Left[i];
34         for(int i=1;i<=n;i++)scanf("%d",&Right[i]),sum+=Right[i];
35         memset(dp,0,sizeof(dp));
36         if(n==1){
37             printf("%d\n",max(Left[1],Right[1]));
38             continue;
39         }
40         printf("%d\n",dfs(1,n,1,n,sum));
41     }
42     return 0;
43 }
View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值