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.

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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; #define MAXN 101
/*
可以向一个方向最多移动k步
因为从一个位置进入所获得的收益是固定的和前面的状态没有关系,所以可以把这个结果记录下来节省时间 记忆化搜索 递归
在上下左右四个方向找 受益最大的格子同时记录
*/
int n,k,g[MAXN][MAXN],dp[MAXN][MAXN];
int dir[][] = {{,},{-,},{,},{,-}};
int Mdfs(int x,int y)
{
if(dp[x][y])
return dp[x][y];
int Max = ;
for(int i=;i<;i++)
{
for(int d=;d<=k;d++)
{
int nx = x + dir[i][]*d;
int ny = y + dir[i][]*d;
if(nx<n&&nx>=&&ny>=&&ny<n&&g[nx][ny]>g[x][y])
Max = max(Max,Mdfs(nx,ny));
}
}
dp[x][y] = Max + g[x][y];
return dp[x][y];
}
int main()
{
while(scanf("%d%d",&n,&k))
{
if(n==-&&k==-) break;
memset(dp,,sizeof(dp));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
scanf("%d",&g[i][j]);
}
printf("%d\n",Mdfs(,));
}
return ;
}

P - FatMouse and Cheese 记忆化搜索的更多相关文章

  1. HDU - 1078 FatMouse and Cheese (记忆化搜索)

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

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

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

  3. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

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

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

  5. [HDOJ1078]FatMouse and Cheese(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n, k,然后给出n*n的地图,(下标0~n-1),有一只老鼠从(0,0)处出发,只能 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 老鼠初始时在n*n的矩阵的(0 , 0)位置,每次可以向垂直或水平的一个方向移动1到k格,每次移 ...

  7. HDU 1078 FatMouse and Cheese (记忆化搜索+dp)

    详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...

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

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

  9. !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)

    题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...

随机推荐

  1. DotnetCore(1)尝鲜构建Web应用

    在上篇文章中DotnetCore环境安装完成后,现在我们来尝试构建Web应用. 新建文件夹NetCoreWebDemo,并cd进入NetCoreWebDemo文件夹 同时Ctrl+shift按下快捷键 ...

  2. tomcat8的session共享实现方案

    tomcat8的session共享实现 下载tomcat 版本:apache-tomcat-8.0.53.zip 实现步骤,只需要两步 两个tomcat的server.xml都增加一样cluster配 ...

  3. [POI2015]Wycieczki

    题目描述 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长度,注意路径不一定是简单路径,即可以重复走同一个点. 输入输出 ...

  4. Node“getTextContent() is undefined for the type Node”处理办法

    最近一个项目在MyEclipse导入后总是报getTextContent() is undefined for the type Node错误. 经过查找原来是因为Node类为JDK中自带的(org. ...

  5. 1、DOS基本命令

    命令dir能给列出当前目录下面的所有文件.程序和子目录.所有目录(Windows 中称为文件夹)的目录名前面都有一个<DIR>标记.文件和程序名前面显示有这些文件和程序的大小. 想说的是, ...

  6. CF811C Vladik and Memorable Trip

    思路: 令dp[i]表示前i个的最大舒适度.则如果区间[j, i](1 < j <= i)满足条件,有如下转移:dp[i] = max(dp[i], dp[j - 1] + cur).其中 ...

  7. mybatis 关联查询

    1.关联的两个实体类 外部类 parent public class Parent{ private String parentId; private String parentName; priva ...

  8. Python+selenium学习(二) selenium 定位不到元素

    转载:https://www.cnblogs.com/tarrying/p/9681991.html tarrying selenium的三种等待时间 //隐式等待(20秒以内没哥一段时间就会去找元素 ...

  9. daxcie

    Database->Edit Current DBMS菜单 修改如下:选中General选项卡,依次打开Script->Sql->Fomat->CaseSensitivityU ...

  10. 搭建Cookie池

    很多时候我们在对网站进行数据抓取的时候,可以抓取一部分页面或者接口,这部分可能没有设置登录限制.但是如果要抓取大规模数据的时候,没有登录进行爬取会出现一些弊端.对于一些设置登录限制的页面,无法爬取对于 ...