zoukankan      html  css  js  c++  java
  • hdu 1078【FatMouse and Cheese】

    http://acm.hdu.edu.cn/showproblem.php?pid=1078人家说是简单题,我说是神题,这个就是大大滴差距\可怜

    神奇的DP+记忆化搜索

    只要将各个方向递归退栈后得到的最大值用dp[x][y](这里的最优就是各个方向上的最大值加上此处的cheese)记录下来,以后如果再次搜索到这个地方时可以直接返回该处的值……

    ps一句:看了别人的题解后才弄出来的……

    代码如下:
     1 #include <cstdio>
     2 #include <cstring>
     3 
     4 int dp[105][105];
     5 int inp[105][105];
     6 int n,k;
     7 int dirx[4] = {-1,0,1,0};
     8 int diry[4] = {0,-1,0,1};
     9 
    10 int dfs(int x,int y)
    11 {
    12     if(dp[x][y] > 0)
    13         return dp[x][y];
    14 
    15     int maxn = 0;
    16     for(int i =0;i < 4;i ++)
    17     {
    18         for(int j = 1;j <= k;j ++)
    19         {
    20             int rx = x + dirx[i] * j;
    21             int ry = y + diry[i] * j;
    22             if(rx < 0 || rx >= n || ry < 0 || ry >= n || inp[rx][ry] <= inp[x][y])
    23                 continue;
    24             int ans = dfs(rx,ry);
    25             if(maxn < ans)
    26                 maxn = ans;
    27         }
    28     }
    29 
    30     dp[x][y] += maxn + inp[x][y];
    31     return dp[x][y];
    32 }
    33 
    34 int main()
    35 {
    36     while(scanf("%d%d",&n,&k) == 2)
    37     {
    38         if(n == -1 || k == -1)
    39             break;
    40 
    41         for(int i = 0;i < n;i ++)
    42         {
    43             for(int j = 0;j < n;j ++)
    44             {
    45                 scanf("%d",&inp[i][j]);
    46             }
    47         }
    48 
    49         memset(dp,0,sizeof(dp));
    50 
    51         dfs(0,0);
    52         printf("%d\n",dp[0][0]);
    53     }
    54 
    55     return 0;
    56 }
  • 相关阅读:
    字符串转换成整型数 atoi()
    求一个正整数各个数位上的数字之和
    求小于等于n的所有素数
    iomanip,setw(),setw: undeclared identifier
    计算机界的牛人前辈
    clrscr( )用法
    printf()
    realloc() 用法详解
    ADO和ADO.NET的区别
    C++中delete和delete[]的区别
  • 原文地址:https://www.cnblogs.com/Shirlies/p/2640284.html
Copyright © 2011-2022 走看看