pid=1078">FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4811    Accepted Submission(s): 1945

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
 

题意是说每次能够走(1~K)个在同一直线的位置,即不能拐弯走。

数组较大,用记忆化搜索。

(类似于poj1088滑雪)

#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"stack"
#include"algorithm"
using namespace std;
#define N 105
#define max(a,b) (a>b?a:b)
int g[N][N],n,k;
int h[N][N];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int judge(int x,int y)
{
if(x<0||x>=n||y<0||y>=n)
return 0;
return 1;
}
int dfs(int x,int y)
{
if(h[x][y])
return h[x][y];
int i,j,u,v,t,sum=0,s1;
t=g[x][y];
for(i=0;i<4;i++)
{
for(j=1;j<=k;j++)
{
u=x+dir[i][0]*j;
v=y+dir[i][1]*j;
if(judge(u,v)&&g[u][v]>t)
{
s1=dfs(u,v);
sum=max(sum,s1);
}
}
}
return h[x][y]=g[x][y]+sum;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&k),n!=-1||k!=-1)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&g[i][j]);
h[i][j]=0;
}
} int ans=dfs(0,0);
printf("%d\n",ans);
}
return 0;
}

hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)的更多相关文章

  1. 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...

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

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

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

    题意:给出n*n的二维矩阵,和k,老鼠每次最多走k步,问老鼠从起点(0,0)出发,能够得到的最大的数(即为将每走过一点的数都加起来的和最大)是多少 和上一题滑雪一样,搜索的方向再加一个循环 #incl ...

  4. HDU - 1078 FatMouse and Cheese(记忆化+dfs)

    FatMouse and Cheese FatMouse has stored some cheese in a city. The city can be considered as a squar ...

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

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

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

    题目链接:点击链接 题目大意:老鼠从(0,0)出发,每次在同一个方向上最多前进k步,且每次到达的位置上的数字都要比上一个位置上的数字大,求老鼠经过的位置上的数字的和的最大值 #include<s ...

  7. hdu 1078 FatMouse and Cheese(记忆搜)

    N*N的矩阵,每个格子上有一个值. 老鼠起始在(1,1),每次只能水平着走或垂直着走.且最多只能走K步.且走到的格子里的值必须比上一次呆的格子里的值大. 问老鼠最多收集到多少值. 思路: 记忆搜好写. ...

  8. HDU 1078 FatMouse and Cheese ( DP, DFS)

    HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...

  9. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  10. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

随机推荐

  1. Linux系统上安装Python

    1.下载Python安装包,官网下:http://www.python.org/getit/ http://jingyan.baidu.com/article/eae07827f7f2d01fec54 ...

  2. 用DzzOffice管理阿里云OSS

    在DzzOffice分两种方式管理阿里云OSS 1.把阿里云oss作为多人或企业的共享网盘使用. 2.接入个人的阿里云oss管理,可同时管理多个bucket,多个bucket之间可以互传文件. 下面先 ...

  3. js正则表达式中=s.g表示什么意思

    //g是全局匹配//中间的内容表示:匹配以=开关,后面是0或多个空格,然后是双引号括起来的任意字符,比如:= "any symble" 匹配 = " asfjaskldf ...

  4. pycharm出现乱码

    1. 'gbk' codec can't encode character u'\xb8' 解决办法 import sys reload(sys)sys.setdefaultencoding('utf ...

  5. 网络爬虫系统Heritrix的结构分析 (个人读书报告)

      摘要 随着网络时代的日新月异,人们对搜索引擎,网页的内容,大数据处理等问题有了更多的要求.如何从海量的互联网信息中选取最符合要求的信息成为了新的热点.在这种情况下,网络爬虫框架heritrix出现 ...

  6. 应用dom4j读取xml的例子

    1. 样例xml <?xml version="1.0" encoding="UTF-8"?> <students> <stude ...

  7. IMAQdx和IMAQ

    NI-IMAQdx driver software gives you the ability to acquire images with IEEE 1394 and GigE Vision cam ...

  8. hdu 5532 Almost Sorted Array

    http://acm.hdu.edu.cn/showproblem.php?pid=5532  题目大意: 给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻 ...

  9. hdu 1199 Color the Ball

    http://acm.hdu.edu.cn/showproblem.php?pid=1199 Color the Ball Time Limit: 2000/1000 MS (Java/Others) ...

  10. 【下有对策】verycd没有的资源有很多方法下载

    由于国内专门从事假冒电驴官方欺骗中国人的verycd公司出品的冒牌官方电驴"阉割驴"目前已经阉割掉了搜索功能,请电驴爱好者们尽快更换正宗版电驴软件: 电骡--emule官方网站:h ...