HDU 1078 FatMouse and Cheese【记忆化搜索】
题意:给出n*n的二维矩阵,和k,老鼠每次最多走k步,问老鼠从起点(0,0)出发,能够得到的最大的数(即为将每走过一点的数都加起来的和最大)是多少
和上一题滑雪一样,搜索的方向再加一个循环
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<algorithm>
using namespace std; typedef long long LL;
int a[][],d[][],n,k;
int dir[][]={,,-,,,,,-}; int dfs(int x,int y){
if(d[x][y]) return d[x][y];//如果已经搜过这一点,则直接返回,不用再重复计算
int ans=;
for(int j=;j<=k;j++){
for(int i=;i<;i++){ //四个 方向搜
int nx=x+dir[i][]*j;
int ny=y+dir[i][]*j;
if(nx<||nx>=n||ny<||ny>=n) continue;//越界
if(a[x][y]<a[nx][ny])
ans=max(ans,dfs(nx,ny));
}
} return d[x][y]=ans+a[x][y];
} int main()
{
while(scanf("%d %d",&n,&k)!=EOF&&n!=-&&k!=-){
for(int i=;i<n;i++)
for(int j=;j<n;j++) cin>>a[i][j]; memset(d,,sizeof(d)); printf("%d\n",dfs(,));
}
return ;
}
HDU 1078 FatMouse and Cheese【记忆化搜索】的更多相关文章
- HDU - 1078 FatMouse and Cheese (记忆化搜索)
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...
- 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 ...
随机推荐
- 生鲜电商的O2O之道
- SQL Server备份事务日志结尾(Tail)
原文:http://blog.csdn.net/tjvictor/article/details/5256906 事务日志结尾经常提交数据库未备份的事务日志内容.基本上,每一次你执行事务日志备份时 ...
- c++ 函数返回指针 及用法
#include<string> #include<iostream> using namespace std; string fun1(int a) { string str ...
- UVALive 6525
二分图最大匹配 #include<cstdio> #include<iostream> #include<cstring> #define MAX 10010 us ...
- Tutorial: Model
What is a model? Across the internet the definition of MVC is so diluted that it's hard to tell what ...
- String类的使用 Part2
StringBuilder 类的使用 属性: namespace StringBuilderTest { class Program { static void Main(string[] args) ...
- lintcode: 二叉树的锯齿形层次遍历
题目 二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / \ ...
- windows下顽固软件卸载不了的解决方法
下面以autocad2012举例: cad2012卸载显示 “无法获得同类产品” 而且也安装不上. 解决方法:1,开始>运行>输入"regedit",找到下面的注册表路 ...
- java:继承
一.继承: java只支持单继承,一个子类只能继承一个父类,使用继承是为了减少类的重复代码,且父类的构造函数不能被子类继承. 当两个类里面有相同的属性或方法,就应该考虑使用继承解决重复代码了. 继承的 ...
- GridView 和ListView中自适应高度
android中GridView 和ListView放在scrollView中时会默认的只有一行高这时就要我们自己计算出它的高度啦 首先是listview的 //动态设置listview的高度 pu ...