FatMouse and Cheese 动态化搜索

FatMouse and Cheese

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

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 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 int n,k;
 9 const int maxn = 105;
10 int maze[maxn][maxn],dp[maxn][maxn];
11 int dfs(int p,int q){
12     if(dp[p][q]) return dp[p][q];
13     int l,r,d,u;
14     int ans = 0,maxx = 0;
15     l = p - k;
16     r = p + k;
17     if(l<0) l = 0;
18     if(r>=n) r = n-1;
19     for(int i = l; i<=r; i++){
20         if(maze[i][q]>maze[p][q]){
21             ans = dfs(i,q);
22             if(ans>maxx) maxx = ans;
23         }
24     }
25     u = q + k;
26     d = q - k;
27     if(d<0) d = 0;
28     if(u>=n) u = n-1;
29     for(int i = d; i<=u; i++){
30         if(maze[p][i]>maze[p][q]){
31             ans = dfs(p,i);
32             if(ans>maxx) maxx = ans;
33         }
34     }
35     dp[p][q] = maxx + maze[p][q];
36     return dp[p][q];
37 }
38 void input(){
39         while(scanf("%d%d",&n,&k)!=EOF&&n != -1&&k != -1){
40             for(int i = 0; i<n; i++)
41                 for(int j = 0; j<n; j++)
42                 scanf("%d",&maze[i][j]);
43             memset(dp,0,sizeof(dp));
44             printf("%d\n",dfs(0,0));
45         }
46 }
47 int main()
48 {
49     input();
50     return 0;
51 }

卷珠帘

时间: 2024-09-30 09:22:45

FatMouse and Cheese 动态化搜索的相关文章

hdu1078 FatMouse and Cheese(记忆化搜索)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 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

hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4811    Accepted Submission(s): 1945 Problem Description FatMouse has stored some cheese in a city. The city can

FatMouse and Cheese (hdu 1078 记忆化搜索)

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5463    Accepted Submission(s): 2227 Problem Description FatMouse has stored some cheese in a city. The city can be considere

HDU1078 FatMouse and Cheese 【内存搜索】

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4966    Accepted Submission(s): 2035 Problem Description FatMouse has stored some cheese in a city. The city can be considere

(记忆化搜索) FatMouse and Cheese(hdu 1078)

题目大意: 给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值 (题目很容易会理解错题意,道友小心) #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std;

HDU1078 FatMouse and Cheese 【记忆化搜索】

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4966    Accepted Submission(s): 2035 Problem Description FatMouse has stored some cheese in a city. The city can be considere

HDU 1078 FatMouse and Cheese(记忆化搜索)

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8610    Accepted Submission(s): 3611 Problem Description FatMouse has stored some cheese in a city. The city can be considere

P - FatMouse and Cheese HDU 1078 ( 记忆化搜索 )

P - FatMouse and Cheese Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1078 Description FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid

HDU 1078 FatMouse and Cheese(DP)

题意  老鼠在一个小镇吃奶酪  城镇可以看成一个n*n的矩阵  其中每个格子都有一定数量的奶酪mat[i][j]   老鼠从(0,0) 开始吃   而且下个吃的格子里的奶酪必须比上个格子多   老鼠只能水平方向或者垂直方向走  而且每次走的距离不能超过k  求老鼠最多能吃多少奶酪 起点是固定的   比较容易   直接记忆化搜索 令d[i][j]表示以(i,j)为终点的最优解  那么对于所有(i,j)能到达的点(x,y)有  d[i][j]=max(d[i][j],d[x][y]+mat[x][y