FatMouse and Cheese

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

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
 
Source
 题意:
n*n的矩阵,从左上角开始,每次最多只能走k步,并且只能走到权值更大的点,问走过的所有的点的最大权值和是多少。
代码:
//普通的搜索会超时,记忆化搜索,当搜到某点的权值和在前面的搜索中已经算过了就直接返回。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x7fffffff;
int f[][],mp[][];
int dir[][]={,,-,,,,,-};
int n,k;
int dfs(int x,int y)
{
if(!f[x][y]){
int tmp=;
for(int j=;j<=k;j++){
for(int i=;i<;i++){
int xx=x+dir[i][]*j,yy=y+dir[i][]*j;
if(xx<||xx>=n||yy<||yy>=n) continue;
if(mp[xx][yy]<=mp[x][y]) continue;
tmp=max(tmp,dfs(xx,yy));
}
}
f[x][y]=tmp+mp[x][y];
}
return f[x][y];
}
int main()
{
while(scanf("%d%d",&n,&k)){
if(n==-&&k==-) break;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
scanf("%d",&mp[i][j]);
}
}
memset(f,,sizeof(f));
int ans=dfs(,);
printf("%d\n",ans);
}
return ;
}

HDU1078记忆化搜索的更多相关文章

  1. hdu1078 记忆化搜索

    /* hdu 1078 QAQ记忆化搜索 其实还是搜索..因为里面开了一个数组这样可以省时间 (dp[x][y]大于0就不用算了直接返回值) */ #include<stdio.h> #i ...

  2. hdu1078  记忆化搜索(DP+DFS)

    题意:一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子上的数必须比上一个走的格子的数大,问最大的路径和. 我一开始的思路是,或许是普通的最大路径和,只 ...

  3. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. hdu1078 FatMouse and Cheese —— 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...

  5. hdu1078 FatMouse and Cheese(记忆化搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意: 题目中的k表示横向或者竖直最多可曾经进的距离,不可以拐弯.老鼠的出发点是(1,1) ...

  6. hdu1078 FatMouse and Cheese(记忆化搜索)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...

  7. 再谈记忆化搜索 HDU-1078

    最近做DP题目,发现无论是LCS,还是有些题目涉及将动态规划的路径打印出来,而且有时候还要按格式输出,这个时候,记忆化搜索显得尤其重要,确实,记忆化搜索使用优化版本的动态规划,用起来思路清晰,非常方便 ...

  8. 专题1:记忆化搜索/DAG问题/基础动态规划

      A OpenJ_Bailian 1088 滑雪     B OpenJ_Bailian 1579 Function Run Fun     C HDU 1078 FatMouse and Chee ...

  9. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

随机推荐

  1. Centos7添加静态路由

    本文摘取自 Centos7系统配置上的变化(二)网络管理基础 一.ip route显示和设定路由 1.显示路由表 [root@centos7 ~]# ip route show default via ...

  2. 如何在线测试Exchange的速度

    最新碰到了客户需要比较国内版和国际版的Office365的速度问题,微软提供在线工具测试 这里以Exchange 测试为例子,请参考. PS Onenote贴过来只能至图片,各位看官只能将就了 这里有

  3. Multi-task Correlation Particle Filter for Robust Object Tracking--论文随笔

    摘要:在这篇论文中,作者提出一种鲁棒视觉跟踪的多任务相关粒子滤波琪跟踪算法(MCPF).作者首先向我们展示了多任务相关滤波器,该滤波器在训练滤波器模板的时候可以学习不同特征之间的联系.本文提出的MCP ...

  4. Machine Learning笔记整理 ------ (四)线性模型

    1. 线性模型 基本形式:给定由d个属性描述的样本 x = (x1; x2; ......; xd),其中,xi是x在第i个属性上的取值,则有: f(x) = w1x1 + w2x2 + ...... ...

  5. 常用算法Java实现之冒泡排序

    冒泡排序是所有排序算法中最基本.最简单的一种.思想就是交换排序,通过比较和交换相邻的数据来达到排序的目的. 具体流程如下: 1.对要排序的数组中的数据,依次比较相邻的两个数据的大小. 2.如果前面的数 ...

  6. Ubuntu16.04修改IP

    首先用root用户登陆,然后输入你root的密码.如下图:   然后编辑interfaces文件,该文件位于/etc/network/下,执行如下命令: vim /etc/network/interf ...

  7. 3ds Max学习日记(六)

      到了周六就不想再忙实验室的活了,于是玩了一下3ds max,第5和第6章每章都只有4个视频,于是就一起弄完了,什么网格建模,曲面建模啥的,nurbs啥的. 附上今日的劳动成果:   叉子(用长方体 ...

  8. 关于&$地址传递的练习

    php默认为传值传递: 既: $a=10;$b=$a; //$b为10$a=+10; //$a 为20 echo $a.'和'.$b;  # $a is 20 and $b is 10! 要是想变为地 ...

  9. Java、JSP与JavaScript的区别

    java和javaScript是两种完全不同的语言,并不是收都有个java就类似 Java,全称应该是 Java Applet,是嵌在网页中,而又有自己独立的运行窗口的小程序.Java Applet ...

  10. 第一篇:python基础_1

    本篇内容 Python介绍 安装 第一个程序(hello,world) 变量 用户输入(input) 数据类型 数据运算 if判断 break和continue的区别 while 循环 一. Pyth ...