HDU - 1078 FatMouse and Cheese (记忆化搜索)
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.
InputThere 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.
OutputFor 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 题意:
一步最多走k步,只能走到比当前值大的位置,问路径的权值最大和是多少?
思路:
相当明显的记忆化搜索,算是一个基本套路。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int dp[][];
int mp[][];
int n,k;
int dfs(int x,int y){
if(dp[x][y]){return dp[x][y];}
int &ret = dp[x][y];
ret=mp[x][y];
for(int i=;i<=k;i++){
if(i+x<=n&&mp[i+x][y]>mp[x][y]){ret=max(ret,dfs(i+x,y)+mp[x][y]);}
if(i+y<=n&&mp[x][i+y]>mp[x][y]){ret=max(ret,dfs(x,i+y)+mp[x][y]);}
if(x-i>=&&mp[x-i][y]>mp[x][y]){ret=max(ret,dfs(x-i,y)+mp[x][y]);}
if(y-i>=&&mp[x][y-i]>mp[x][y]){ret=max(ret,dfs(x,y-i)+mp[x][y]);} }
return ret;
}
int main()
{
while(scanf("%d%d",&n,&k)&&(~n)&&(~k)){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&mp[i][j]);
dp[i][j]=;
}
}
dfs(,);
printf("%d\n",dp[][]);
}
return ;
}
HDU - 1078 FatMouse and Cheese (记忆化搜索)的更多相关文章
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...
- HDU 1078 FatMouse and Cheese (记忆化搜索+dp)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...
- hdu 1078 FatMouse and Cheese 记忆化dp
只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...
- HDU ACM 1078 FatMouse and Cheese 记忆化+DFS
题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...
- hdu1078 FatMouse and Cheese —— 记忆化搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #in ...
- P - FatMouse and Cheese 记忆化搜索
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
随机推荐
- git 的 origin 的含义
我们从progit 一书中可以看到: 远程仓库名字 “origin” 与分支名字 “master” 一样,在 Git 中并没有任何特别的含义一样. 同时“master”是当你运行git init时默认 ...
- 深入理解Mysql索引的底层数据结构 B+ Tree (1)
关键字的个数等于路的个数减1. 一个二叉树节点可以存储4kb大小的数据,假如关键字是整型的一个关键字占用4byte,其他数据冗余4个字节 4 kb = 4*1024 byte = 4096 byte. ...
- CSS---内外边距
1.内外边距含义 内边距是div边框内的距离.背景色会覆盖内边距,内边距会使宽高变大. 外边距是div边框外的距离.背景色不会覆盖外边距 内外边距都会撑高父元素,外边距会提高div与div之间的距离 ...
- HBase实践案例:知乎 AI 用户模型服务性能优化实践
用户模型简介 知乎 AI 用户模型服务于知乎两亿多用户,主要为首页.推荐.广告.知识服务.想法.关注页等业务场景提供数据和服务, 例如首页个性化 Feed 的召回和排序.相关回答等用到的用户长期兴趣特 ...
- CentOS 7 中使用NTP进行时间同步
1. NTP时钟同步方式说明NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步: 直接同步 使用ntpdate命令进行同步,直接进行时间变更.如果服务器上存在一个12点运行的任务,当前服 ...
- Nodejs OracleDB详细解读
//导入oracledb模块 //基于版本@3.0.1 安装指令npm install oracledb //node访问oracleDB需要搭建访问环境,否则无法正常访问 //创建Oracle对象 ...
- Redtiger SQL注入练习(一)
感觉会的东西太少了,以后要多练习,多写博客.要坚持学习,一定不能放弃,为梦想奋斗. redtiger 这个平台早就开始做了,但是才做到第4关.... 第一关: 打开题, 先随便试,后来发现点击 Ca ...
- Linux内存管理 (17)KSM
专题:Linux内存管理专题 关键词:KSM.匿名页面.COW.madvise .MERGEABLE.UNMERGEABLE. KSM是Kernel Samepage Merging的意思,用于合并内 ...
- DP求树的重心
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- MySQL索引原理及慢查询优化(转自:美团tech)
背景 MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会 ...