zoukankan      html  css  js  c++  java
  • hdu 1078 FatMouse and Cheese (dfs + dp)

    题目:

    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
     

     要求:   ① 起点在(0,0),每次 最多 能跳 k 格

              ② 下一个位置的 cheese 要比当前位置多 才能跳过去吃掉

              ③ 求最多 能吃多少

    第二次写这种思路的题。

    第一次是比赛的题  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3644

    (看了学长的代码,学会的这种方法)

    思路 :

             从起点开始搜,如果直接搜的话,每次跳的格数不定,所以情况较多。

         其实这样会有重复的步骤,就可以用一个dp数组记录当前位置的最优值,下次搜到的时候直接用就行,可以省很多时间。

    代码 :

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 int dp[105][105],map[105][105];
     6 int n,k,sum;
     7 
     8 int dfs( int x, int y ) {
     9 
    10     if( dp[x][y]!=-1 ) return dp[x][y];
    11 
    12     int Max=-1,flag=0;
    13 
    14     for( int i=1;i<=k;i++) {
    15 
    16         if( y+i<=n )
    17             if( map[x][y+i]>map[x][y] )
    18             {
    19                 flag=1;
    20                 Max= max( Max ,dfs(x,y+i)+map[x][y] );
    21             }
    22         if( y-i>=1 )
    23             if( map[x][y-i]>map[x][y] )
    24             {
    25                 flag=1;
    26                 Max= max( Max ,dfs(x,y-i)+map[x][y] );
    27             }
    28         if( x+i<=n )
    29             if( map[x+i][y]>map[x][y] )
    30             {
    31                 flag=1;
    32                 Max= max( Max ,dfs(x+i,y)+map[x][y] );
    33             }
    34         if( x-i>=1 )
    35             if( map[x-i][y]>map[x][y] )
    36             {
    37                 flag=1;
    38                 Max= max( Max ,dfs(x-i,y)+map[x][y] );
    39             }
    40     }
    41 
    42     if( !flag )  dp[x][y]=map[x][y];
    43     else  dp[x][y]=Max;
    44 
    45     return dp[x][y];
    46 }
    47 
    48 
    49 int main( ){
    50 
    51     while( 1 ) {
    52         cin >> n>>k;
    53         if( n==-1 && k==-1 ) break;
    54         sum=0;
    55         memset(dp,-1,sizeof(dp));
    56         for( int i=1;i<=n;i++)
    57            for( int j=1;j<=n;j++)
    58                cin >>map[i][j];
    59         cout << dfs(1,1)<<endl;
    60     }
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    Ext Js MVC系列二 利用Application和Viewport进行应用程序初始化和页面布局
    LINQ to Sql系列一 增,删,改
    Ext Js MVC系列一 环境搭建和MVC框架整体认识
    LINQ to Sql系列四 性能优化总结
    SQL基础回顾系列一 单表查询(select语句)
    JSON详解
    公用类库(4) 缓存操作类CacheUtil
    架构设计考虑的问题(出自代码大全II)
    .net自动更新组件Ant
    .net socket在win2008下的吞吐性能报告
  • 原文地址:https://www.cnblogs.com/lysr--tlp/p/ppppp.html
Copyright © 2011-2022 走看看