【记忆搜索法】FatMouse and Cheese

来源:互联网 发布:程序员培训学校 编辑:程序博客网 时间:2024/06/01 09:33

国际惯例Po主叨一叨,今天做题又暴露出了许多问题,果然Po还是个小菜鸡啊...

首先抛开题目不谈,只谈记忆搜索法,dp,说到底,这个dp数组到底是用来干什么的?

 里面存的应该是通过dfs算出的从这个点出发能得到的最大值!
 只是第一次dfs(x,y)都要递归很多次才能得到从(x,y)这个点出能获得的最大值,如果把这个值存到dp里,下一次dfs只需要访问dp的值就行,不用再递归!

做这个题的时候,一开始我用循环去找这个矩阵里的最大值,超时了一遍又一遍,这很明显就是对dp数组的意义理解得不够到位。

OK,上题目上代码:

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 Specification

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 Specification

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

Output for Sample Input

37

-------------------------

#include <cstdio>#include <iostream>#include <algorithm>#include <string.h>using namespace std;int n,k;int dp[105][105];int maps[105][105];int MAX=0;int dir[4][2]={1,0,0,1,-1,0,0,-1};int max(int x,int y){if(x>y)return x;elsereturn y;}int dfs(int x,int y){int tx,ty;int i,j;int MAXX=0;int t;if(dp[x][y]) return dp[x][y];for(i=0;i<4;i++){for(j=1;j<=k;j++){tx=x+dir[i][0]*j;//因为只能走一个方向ty=y+dir[i][1]*j;if(tx>=0 && tx<n && ty>=0 && ty<n){if(maps[tx][ty]>maps[x][y]){t=dfs(tx,ty);//找到值;MAXX=max(MAXX,t);//把较大的值保存在MAXX里}}}}dp[x][y]=MAXX+maps[x][y];//最大的MAXX值和当前的maps里的值相加才是dp应该保存的值return dp[x][y];}int main(){int i,j;while(~scanf("%d %d",&n,&k)){if(n==-1 && k==-1)break;memset(dp,0,sizeof(dp));for(i=0;i<n;i++){for(j=0;j<n;j++){scanf("%d",&maps[i][j]);}}printf("%d\n",dfs(0,0));}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 绝地求生次激战场机型不支持怎么办 木茷生存中文版安装包损坏了怎么办 手机下载软件显示安装包损坏怎么办 不小心把qq图片删了怎么办 奶水不足宝宝不好好吸奶怎么办 膀胱切除前列腺切除阴茎不硬怎么办 小孩的睾丸睾丸碰肿了怎么办 怎么判断小孩子的睾丸没下来怎么办 怀孕39周腰酸屁股酸疼该怎么办 我儿子18岁睾丸筋鼓起来怎么办 去医院检查说精子跑的慢怎么办 多囊卵巢综合症引起屁股增大怎么办 蚊子咬了肿了挠破了流水怎么办 血糖高引发的睾丸一直烂怎么办? 被洪水淹过的猪后期怎么办 做睾丸阴囊彩超阴茎突然勃起怎么办 阴茎冠状沟皮肤感染总不愈合怎么办 不小心咬到孩子破皮了怎么办 小孩子不小心碰到脸黑了一块怎么办 眼睛不小心碰到了里面红了怎么办 八个月宝宝睾丸还沒掉下来怎么办 孩子背部皮肤有一块皮肤很脏怎么办 洗浴种心搓背老板不给发工资怎么办 半个月小鸡屁骨下垂眼睛紧闭怎么办 在学校走廊把老师撞倒了怎么办 裤衩给孩子买的有点肥怎么办 到交警队立案后医疗费没了怎么办 交警扣车车里的贵重东西没了怎么办 睾丸内囊肿割了又长怎么办 我把别人的卵子踢碎了怎么办 两岁宝宝不小心被猫抓出血怎么办 两岁宝宝小蛋蛋肿了怎么办 吃大胺片过敏蛋皮又痛又痒怎么办 刚出生二十天宝宝蛋蛋有疝气怎么办 宝宝八个月蛋蛋一个没掉下来怎么办 引产23天同房内射肚子疼怎么办l 房东禁止养宠物如果养了怎么办 圆通快递退回但没有签收记录怎么办 信用卡没有收到又退回去了怎么办 新疆不给邮寄快递被退回运费怎么办 淘宝上已付款还在想留言怎么办