再谈记忆化搜索

题目描述:
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
思路分析:
题目大概的意思就是输入一个n和一个k,n代表地图维度,k代表每次走的最大步数,求一条路径上的数值最大和。本题需要注意,只能沿着一个方向走,不能出现拐弯的情况,即只能横着走,或者竖着走,且只能走比上一个点数值大的点。
大概实现:

for(int i=0;i<4;i++){for(int j=1;j<=m;j++){tx=x+dir[i][0]*j;ty=y+dir[i][1]*j;

既然是记忆化搜索,那么一定要用到DP的思路,dp在这里面表示什么呢?他表示的是该点到终点经过的每条路径上和最大值。即答案可用dp[1][1]来表示,当然我们dfs(1,1)也是答案,后面就知道了。思路就是,枚举在当前点能走到的每一个点,然后记录下每次的答案,即temp,最后在回溯的过程中,我们要选出最大值。这个过程中我们有个剪枝,即依据题意,比上一次小的点不走。
dfs代码如下:

int dfs(int x,int y){int tx,ty,kmax=0;if(dp[x][y]!=-1) return dp[x][y];for(int i=0;i<4;i++){for(int j=1;j<=m;j++){tx=x+dir[i][0]*j;ty=y+dir[i][1]*j;if(check(tx,ty)&&a[tx][ty]>a[x][y]){temp=dfs(tx,ty);if(kmax<temp) kmax=temp;}}}dp[x][y]=a[x][y]+kmax;return dp[x][y];
}

完整代码展示:

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int n,m,temp;
int a[105][105];
int dp[105][105];
int check(int x,int y){if(x<1||y<1||x>n||y>n) return 0;else return 1;
}
int dfs(int x,int y){int tx,ty,kmax=0;if(dp[x][y]!=-1) return dp[x][y];for(int i=0;i<4;i++){for(int j=1;j<=m;j++){tx=x+dir[i][0]*j;ty=y+dir[i][1]*j;if(check(tx,ty)&&a[tx][ty]>a[x][y]){temp=dfs(tx,ty);if(kmax<temp) kmax=temp;}}}dp[x][y]=a[x][y]+kmax;return dp[x][y];
}
int main(){while(cin>>n>>m){if(n==-1&&m==-1) return 0;memset(dp,-1,sizeof dp);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>a[i][j];cout<<dfs(1,1)<<endl;}return 0;
}

好哒,就是这些了,嘻嘻嘻嘻~

HDU - 1078 FatMouse and Cheese相关推荐

  1. HDU 1078 FatMouse and Cheese【记忆化搜索】

    <题目链接> 题目大意: 给你一个n*n的矩阵,每个矩阵上有相应数量的奶酪,老鼠一次最多走K步,且每次只能横着走或者竖着走,并且每一次停留位置上的奶酪数一定要多余它刚才的奶酪数,求这只老鼠 ...

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

    题意:一个n*n的矩阵,每个点有若干块点心,小老鼠开始在左上角,每次最多往前走k步,且停留的点的点心数比上一次停留的点大,输出最大的i点心数 分析:停留的点数目必须比上次大,形成一个序,必定是一个DA ...

  3. HDUOJ 1078 FatMouse and Cheese

    HDUOJ 1078 FatMouse and Cheese 题目链接 Problem Description FatMouse has stored some cheese in a city. T ...

  4. HDU ACM 1078 FatMouse and Cheese 记忆化+DFS

    题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...

  5. hdu 1078 FatMouse Chees

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目描述: Description FatMouse has stored some chees ...

  6. 动态规划训练21 [FatMouse and Cheese HDU - 1078 ]

    FatMouse and Cheese HDU - 1078 这道题需要说一说,定义dp[x][y]表示从点(x,y)出发,每次走不超过k步,所能吃到的最大量. 有点难搞的是,这里递归的顺序不好确定, ...

  7. POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  8. HDU.1009 FatMouse' Trade

    文章目录 一.题目解读 1.原题 2.分类 3.题意 4.输入输出格式 5.数据范围 二.题解参考 1.总体思路 2.思路① (1).分析 (2).一些数据 (3).AC代码 三.评价与后话 1.评价 ...

  9. zoj 1107 FatMouse and Cheese 逆向动态规划

    FatMouse and Cheese 开始试着用深度搜索做,超时了(对于时间复杂度没有概念) 这道题用动态规划.如果用自顶向下的方法,我们不知道结尾的是哪个点,所以不方便用. 如果我们采用自下而上的 ...

最新文章

  1. 新疆弃光量下降14% 弃光问题仍然难解
  2. HTML添加首页,添加首页分类推荐.html
  3. Linux / pthread_create() 函数所使用的线程函数为什么必须是静态函数?
  4. 基于AVS2的图片容器——TPG:现状与改进之路
  5. unittest所有断言方法
  6. 为什么很多大老板银行贷款几千万,看起来还那么潇洒?
  7. 如何写出好的Java代码?
  8. c#使用CefSharp开发winform——环境搭建
  9. Coursera | Introduction to Data Analytics(IBM) | Final Assignment
  10. 前端用户体验提升系列(一)最常见的用户体验指标和提升方式
  11. java 数据权限控制_数据权限-数据列权限设计方案
  12. 如何申请微信公众号及使用操作方法说明
  13. APP开发:线上教育APP盈利模式分析
  14. NIST伪随机测试出现igamc:UNDERFLOW的原因以及测试文件的格式
  15. Linux查看文本中关键字的行
  16. WIN11如何一键返回桌面
  17. 时序建模:时间戳与时序特征衍生思路汇总
  18. 抖音关键词排名怎么靠前,抖音关键词怎么优化?
  19. labelcommand打印条码_VB应用程序中打印条形码的方法
  20. 静态链表(C++实现)——基于数据结构(沈俊版)(初学者食用)

热门文章

  1. 亚马逊测评自养号技能知识共享
  2. JAVA调用有道API接口对数据库中的中文语句进行翻译
  3. python 文件和文件夹操作
  4. Numpy 数组的其他函数--索引argwhere、去重unique、排序sort
  5. 数学问题(四)——素数
  6. Java_Web实战(一) --环境搭建
  7. 贵州支教之第三天(11月9日)
  8. [Linux]-基础知识及命令学习
  9. Android 汇集CSDN、GitHub等最实用的良心之作-KING
  10. 集原美 萝莉少女 电脑4k壁纸3840x2160