HDU 1078 记忆化搜索
2015-08-12 00:05
531 查看
FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6454 Accepted Submission(s): 2627
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)出发,开始遍历每一条可能的路径,当遍历到周围能达到的数都比自己小时返回。记录此时的总值,与变量Max比较,记下较大的数,最终的Max就是要求的值。
自底向上分析:
当遍历到最后时,返回这时的元素值到上一层。对中间的过程用动态规划考虑,dp[i][j]代表的是(i,j)能延伸的最大距离和,转移方程dp[i][j]=max{dp[x][y]}+G[i][j] (x,y为从i,j能延伸到的范围)接着将找到结果点存到dp[]里,下次只要dp[]不为0,就直接调用dp[]而不用递归。
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6454 Accepted Submission(s): 2627
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)出发,开始遍历每一条可能的路径,当遍历到周围能达到的数都比自己小时返回。记录此时的总值,与变量Max比较,记下较大的数,最终的Max就是要求的值。
自底向上分析:
当遍历到最后时,返回这时的元素值到上一层。对中间的过程用动态规划考虑,dp[i][j]代表的是(i,j)能延伸的最大距离和,转移方程dp[i][j]=max{dp[x][y]}+G[i][j] (x,y为从i,j能延伸到的范围)接着将找到结果点存到dp[]里,下次只要dp[]不为0,就直接调用dp[]而不用递归。
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> //#define LOCAL using namespace std; int dp[105][105]; int G[105][105]; const int V[4][2] = {0,-1,0,1,-1,0,1,0}; int n,k; int dfs(int i,int j); bool IsTrue(int a,int b); int main() { #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif // LOCAL while(scanf("%d%d",&n,&k)&&n!=-1&&k!=-1){ int i,j; for(i = 0;i<n;i++) for(j = 0;j<n;j++) scanf("%d",&G[i][j]); memset(dp,0,sizeof(dp)); printf("%d\n",dfs(0,0)); } return 0; } bool IsTrue(int a,int b){ if(0<=a&&a<n&&0<=b&&b<n) return true; return false; }; int dfs(int i,int j){ int t,T; int sum = 0; int ans; if(dp[i][j] != 0) return dp[i][j]; else{ for(T = 0;T<4;T++) for(t = 1;t<=k;t++) if(IsTrue(i+t*V[T][0],j+t*V[T][1])&&G[i][j]<G[i+t*V[T][0]][j+t*V[T][1]]){ ans = dfs(i+t*V[T][0],j+t*V[T][1]); if(ans>sum) sum = ans; } return dp[i][j] = sum + G[i][j]; } }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- JavaScript中数组的排序、乱序和搜索实现代码
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- mysql 模糊搜索的方法介绍
- C#使用动态规划解决0-1背包问题实例分析
- 基于ASP.NET的lucene.net全文搜索实现步骤