zoukankan      html  css  js  c++  java
  • Hdu 4597记忆化搜索

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

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

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

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


    我的代码如下:

     1 /*************************************************************************
     2     > File Name: 4597.c
     3     > Author: Stomach_ache
     4     > Mail: 1179998621@qq.com 
     5     > Created Time: 2014年03月02日 星期日 13时04分27秒
     6     > Propose: 
     7  ************************************************************************/
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <stdlib.h>
    11 
    12 
    13 #define max(x, y) ((x) > (y) ? (x) : (y))
    14 #define min(x, y) ((x) < (y) ? (x) : (y))
    15 
    16 
    17 int a[22], b[22], dp[22][22][22][22], n, t;
    18 int sum_a[22], sum_b[22];
    19 
    20 
    21 int
    22 dfs(int f1, int e1, int f2, int e2) {
    23 
    24 
    25 if (dp[f1][e1][f2][e2] != -1)
    26 return dp[f1][e1][f2][e2];
    27 if (f1 > e1 && f2 > e2) 
    28 return dp[f1][e1][f2][e2] = 0;
    29 
    30 
    31 int res = 0, sum = 0;
    32 if (f1 <= e1) sum += sum_a[e1] - sum_a[f1-1];
    33 if (f2 <= e2) sum += sum_b[e2] - sum_b[f2-1];
    34 
    35 
    36 if (f1 <= e1) {
    37 res = max(res, sum - dfs(f1+1, e1, f2, e2));
    38 res = max(res, sum - dfs(f1, e1-1, f2, e2));
    39 }
    40 if (f2 <= e2) {
    41 res = max(res, sum - dfs(f1, e1, f2+1, e2));
    42 res = max(res, sum - dfs(f1, e1, f2, e2-1));
    43 }
    44 
    45 
    46 return dp[f1][e1][f2][e2] = res;
    47 }
    48 
    49 
    50 int
    51 main(void) {
    52 
    53 
    54 scanf("%d", &t);
    55 while ( t-- ) {
    56 scanf("%d", &n);
    57 int i;
    58 sum_a[0] = sum_b[0] = 0;
    59 for (i = 1; i <= n; i++) {
    60 scanf("%d", a+i);
    61 sum_a[i] = sum_a[i-1]+a[i];
    62 }
    63 for (i = 1; i <= n; i++) {
    64 scanf("%d", b+i);
    65 sum_b[i] = sum_b[i-1]+b[i];
    66 }
    67 
    68 
    69 memset(dp, -1, sizeof(dp));
    70 printf("%d
    ", dfs(1, n, 1, n));
    71 }
    72 
    73 
    74 return 0;
    75 }


  • 相关阅读:
    例5-6
    例5-5
    例5-4
    例4-5
    例4-4
    例4-3
    例4-2
    例3-11
    例3-10
    例3-9
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3703269.html
Copyright © 2011-2022 走看看