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. 整数类型c++

    数据类型 定义标识符 占字节数 数值范围 数值范围 短整型 short [int] 2(16位) -32768-32767 -215-215-1 整型 [long] int 4(32位) -21474 ...

  2. [C陷阱和缺陷] 第7章 可移植性缺陷

      C语言在许多不同的系统平台上都有实现.的确,使用C语言编写程序的一个首要原因就是,C程序能够方便地在不同的编程环境中移植.   不同的系统有不同的需求,因此我们应该能够预料到,机器不同则其上的C语 ...

  3. c++自动导出lua绑定

    cocos 使用bindings-generator脚本代替了toLua++. 编写效率大大提高. 具体的在本机中分享:http://note.youdao.com/noteshare?id=0f41 ...

  4. linux环境mxnet 安装

    环境ubuntu 16.04 1.安装依赖项: sudo update && sudo apt-get install -y build-essential git libatlas- ...

  5. 冒泡 [Python]

    冒泡Python class BubbleSort: def __init__(self): self.initArr() def initArr(self): self.arrInfo = [60, ...

  6. Android ListView绑定数据

    ListView绑定数据的三层: ListView绑定数据源从逻辑上可以分为三层:UI层,逻辑层,数据层. UI层:UI层即是ListView控件. 数据层:要展示到ListView控件上面的数据. ...

  7. JPQL 模糊查询,查询条件拼接(like使用)

    @Transactional public List<ViewCorplist2> findAllCorpsLikeK(String kw) { System.out.println(kw ...

  8. Windows开启ICMP包回显

  9. 安卓app测试之Monkey测试

    一.Monkey特点 1.运行时机:一般是产品稳定后 首轮功能测试完成的夜间进行 2.需要知道packageName 3.目的:主要测试产品是否存在崩溃问题和ANR问题. 二.获取包名的两个方法 首先 ...

  10. 梦想CAD控件网页版关于自定义命令

    在CAD控件操作中,为方便使用者,使用自定义命令发出命令,完成CAD绘图,修改,保存等操作.点击此处在线演示. _DMxDrawX::RegistUserCustomCommand 向CAD控件注册一 ...