hdu1078_FatMouse and Cheese记忆化搜索
来源:互联网 发布:jquery储存数据 编辑:程序博客网 时间:2024/05/17 22:02
FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8725 Accepted Submission(s): 3661
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.
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.
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 11 2 510 11 612 12 7-1 -1
Sample Output
37
题意:给你一个二维数组,从1,1开始走,每次只能往上下或者左右权值更大的点走,每次可以走1~k格,问最大的权值和是多少?
思路:记忆化搜索。注意要控制好底层条件。
#include<bits/stdc++.h>#define INF 0xfffffffusing namespace std;int n,k;int a[105][105];int dp[105][105];int dfs(int i, int j){ if(dp[i][j] != -INF)return dp[i][j]; for(int l = max(1,i-k); l <= min(n,i+k); l++) if(a[i][j] > a[l][j]){ int kk = dfs(l,j); if(kk > 0 && dp[i][j] <0) dp[i][j] = 0; dp[i][j] = max(dp[i][j],kk+a[i][j]); } for(int l = max(1,j-k); l <= min(n,j+k); l++) if(a[i][j] > a[i][l]){ int kk = dfs(i,l); if(kk > 0 && dp[i][j] < 0) dp[i][j] = 0; dp[i][j] = max(dp[i][j],kk+a[i][j]); } return dp[i][j];}int main(){ while(scanf("%d%d",&n,&k)){ for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dp[i][j] = -INF; if(n == -1 && k == -1)break; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d",&a[i][j]); dp[1][1] = a[1][1]; int maxx = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dfs(i,j); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) maxx = max(maxx,dp[i][j]); cout << maxx << endl; }}
0 0
- HDU1078_FatMouse and Cheese【记忆化搜索】
- hdu1078_FatMouse and Cheese记忆化搜索
- zoj1107FatMouse and Cheese (记忆化搜索)
- hdu1078 FatMouse and Cheese(记忆化搜索)
- HDU1078 FatMouse and Cheese 【记忆化搜索】
- hdoj1078 FatMouse and Cheese【记忆化搜索】
- hdu1078 FatMouse and Cheese 记忆化搜索
- HDU1078 FatMouse and Cheese记忆化搜索
- hdu1078FatMouse and Cheese【记忆化搜索】
- hdoj1078 FatMouse and Cheese(记忆化搜索)
- zoj1107FatMouse and Cheese 记忆化搜索
- FatMouse and Cheese(记忆化搜索)
- hdu1078 fatmouse and cheese 记忆化搜索
- hdu1078 FatMouse and Cheese【记忆化搜索】
- hdu 1078 fatmouse and cheese 记忆化搜索
- hdu 1078 FatMouse and Cheese(深搜----记忆化搜索)
- hdu 1078 FatMouse and Cheese(dp 记忆化搜索)
- hdu 1078 FatMouse and Cheese(记忆化搜索)
- 初学Java者对循环的理解(几道基础题)
- 使用Threejs开发WebGL(一)
- Eclipse安装FatJar插件提示Failed to prepare partial IU
- 欢迎加入我们的Java技术交流群
- iOS中block浅谈
- hdu1078_FatMouse and Cheese记忆化搜索
- Linux进程通信
- MySQL数据库提示:Communications link failure,The last packet succe
- [Linux C 编程]进程间通信(一)
- 在字符数组中容易忽视的一些小的问题
- python随意学学
- SIFT四部曲之——高斯滤波
- JavaScript 变量那些事儿
- hdu 1166