zoukankan      html  css  js  c++  java
  • hdu 1078 FatMouse and Cheese 记忆化搜索/DP

    FatMouse and Cheese

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 3   Accepted Submission(s) : 3

    Font: Times New Roman | Verdana | Georgia

    Font Size:  

    Problem Description

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he's going to enjoy his favorite food.

    FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.

    Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move. 

    Input

    There are several test cases. Each test case consists of 

    a line containing two integers between 1 and 100: n and k 
    n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), ... (1,n-1), and so on. 
    The input ends with a pair of -1's. 

    Output

    For each test case output in a line the single integer giving the number of blocks of cheese collected. 

    Sample Input

    3 1
    1 2 5
    10 11 6
    12 12 7
    -1 -1
    

    Sample Output

    37

    从(x1,y1)到(x2,y2)满足(x1==x2)&& |y1-y2|<=k  或者 |x1-x2|<=k && (y1==y2);
    记忆化搜索
    代码:
    View Code
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 
     6 int map[110][110];
     7 int dp[110][110];
     8 int n,m,k;
     9 
    10 int dfs(int p,int q)
    11 {
    12     if(!dp[p][q])
    13     {
    14         int i,j;
    15         int ans,maxn=0;
    16         int left,right,down,up;
    17         left=p-k;        //每一个操作不能超过K步
    18         right=p+k;
    19         if(left<0) left=0;        //防止越界
    20         if(right>=n) right=n-1;
    21         for(i=left;i<=right;i++)
    22         {
    23             if(map[i][q]>map[p][q])        //条件要求下一步必须比前一步的值大
    24             {
    25                 ans=dfs(i,q);        
    26                 if(maxn<ans)
    27                     maxn=ans;
    28             }
    29         }
    30         up=q-k;
    31         down=q+k;
    32         if(up<0) up=0;
    33         if(down>=n) down=n-1;
    34         for(i=up;i<=down;i++)
    35         {
    36             if(map[p][i]>map[p][q])
    37             {
    38                 ans=dfs(p,i);
    39                 if(maxn<ans)
    40                     maxn=ans;
    41             }
    42         }
    43         dp[p][q]=maxn+map[p][q];        //状态转移方程 DP[p][q]=max(dp[i][q],dp[p][j])+map[p][q]  其中i就是左右,j是上下
    44     }
    45     return dp[p][q];
    46 }
    47 
    48 int main()
    49 {
    50     int i,j;
    51     while(1)
    52     {
    53         scanf("%d%d",&n,&k);
    54         if(n==-1 && k==-1) break;
    55         for(i=0;i<n;i++)
    56         {
    57             for(j=0;j<n;j++)
    58                 cin>>map[i][j];
    59         }
    60         memset(dp,0,sizeof(dp));
    61         printf("%d\n",dfs(0,0));
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    P2325 [SCOI2005]王室联邦
    P2709 小B的询问
    P4867 Gty的二逼妹子序列
    P4396 [AHOI2013]作业
    CF617E XOR and Favorite Number
    P4462 [CQOI2018]异或序列
    p4434 [COCI2017-2018#2] ​​Usmjeri
    LOJ 117 有源汇有上下界最小流
    P4137 Rmq Problem / mex
    LOJ 116 有源汇有上下界最大流
  • 原文地址:https://www.cnblogs.com/shenshuyang/p/2639278.html
Copyright © 2011-2022 走看看