HDU 1078 FatMouse and Cheese ----------mDFS

FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2829    Accepted Submission(s): 1098


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
 
 1 /*
 2     这题我当初读时候的理解是:每次从一个点出发最多走k步,但是每步可以上下左右都可以走,当时不知道咋写。
 3 后来看别人的解题报告,都是从一个点可以向四个方向朝直线最多走k步,这样就简单多了。
 4 但是我觉得题目意思没有这么明确啊!。。。。。难道真是自己英语太垃圾了??怪不得六级考两次都不过。。哎!!
 5     解题思路用的是记忆化搜索,就不解释了。。。
 6 */
 7 #include<stdio.h>
 8 
 9 int n,k;
10 int map[100][100],dp[100][100];
11 
12 int mDFS(int i,int j)
13 {
14     if(dp[i][j])
15         return dp[i][j];
16     int left=j-k;
17     int right=j+k;
18     int up=i-k;
19     int down=i+k;
20     if(left<0)
21         left=0;
22     if(right>=n)
23         right=n-1;
24     if(up<0)
25         up=0;
26     if(down>=n)
27         down=n-1;
28     int max=0;
29     for(int p=left;p<=right;++p)
30     {
31         if(map[i][p]>map[i][j])
32         {
33             int tmp=mDFS(i,p);
34             if(max<tmp)
35                 max=tmp;
36         }
37     }
38     for(p=up;p<=down;++p)
39     {
40         if(map[p][j]>map[i][j])
41         {
42             int tmp=mDFS(p,j);
43             if(max<tmp)
44                 max=tmp;
45         }
46     }
47     dp[i][j]=max+map[i][j];
48     return dp[i][j];
49 }
50 
51 int main()
52 {
53     while(scanf("%d%d",&n,&k)&&(n!=-1||k!=-1))
54     {
55         for(int i=0;i<n;++i)
56             for(int j=0;j<n;++j)
57             {
58                 scanf("%d",&map[i][j]);
59                 dp[i][j]=0;
60             }
61         printf("%d\n",mDFS(0,0));
62     }
63     return 0;
64 }

 

你可能感兴趣的:(HDU)