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
原文地址:https://www.cnblogs.com/lysr--tlp/p/ppppp.html