Hdu 4597记忆化搜索

好久没有做题了,水平已经完全在学弟之下了。

一个吉林邀请赛最水的题目。:(

其实这题一看到数据范围,只可以想到思路,直接爆搜,加个记忆化。

这题虽然A了,但是我还是没太想清楚一些边界情况,心虚着A了。


我的代码如下:

/*************************************************************************
    > File Name: 4597.c
    > Author: Stomach_ache
    > Mail: 1179998621@qq.com 
    > Created Time: 2014年03月02日 星期日 13时04分27秒
    > Propose: 
 ************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))


int a[22], b[22], dp[22][22][22][22], n, t;
int sum_a[22], sum_b[22];


int
dfs(int f1, int e1, int f2, int e2) {


if (dp[f1][e1][f2][e2] != -1)
return dp[f1][e1][f2][e2];
if (f1 > e1 && f2 > e2) 
return dp[f1][e1][f2][e2] = 0;


int res = 0, sum = 0;
if (f1 <= e1) sum += sum_a[e1] - sum_a[f1-1];
if (f2 <= e2) sum += sum_b[e2] - sum_b[f2-1];


if (f1 <= e1) {
res = max(res, sum - dfs(f1+1, e1, f2, e2));
res = max(res, sum - dfs(f1, e1-1, f2, e2));
}
if (f2 <= e2) {
res = max(res, sum - dfs(f1, e1, f2+1, e2));
res = max(res, sum - dfs(f1, e1, f2, e2-1));
}


return dp[f1][e1][f2][e2] = res;
}


int
main(void) {


scanf("%d", &t);
while ( t-- ) {
scanf("%d", &n);
int i;
sum_a[0] = sum_b[0] = 0;
for (i = 1; i <= n; i++) {
scanf("%d", a+i);
sum_a[i] = sum_a[i-1]+a[i];
}
for (i = 1; i <= n; i++) {
scanf("%d", b+i);
sum_b[i] = sum_b[i-1]+b[i];
}


memset(dp, -1, sizeof(dp));
printf("%d\n", dfs(1, n, 1, n));
}


return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值