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

思路:
特别记得还在大一下学期军训(5月份)的时候,第一次接触到dfs和bfs的相关题目,当时做了用了好长时间才勉强能做出几道,现在这个题看了下题解基本就掌握套路了
对于老鼠所到达的每个位置,它下一步可能到达的位置有上下左右4个方向分别走w(1=<w<=k)步,方向用t数组来记录

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; int n,k;
int map[][];
int dp[][];
int t[][]={,,-,,,-,,};
int max(int a,int b) {
return a>b?a:b;
} int dfs(int x,int y)
{
int maxn = ;
int ans;
if(dp[x][y] != -) return dp[x][y];
else {
for(int i = ;i <= k;i++)
for(int j = ;j < ;j++)
{
int new_x = x+t[j][]*i;
int new_y = y+t[j][]*i;
if(new_x>=&&new_x<n&&new_y>=&&new_y<n&&map[new_x][new_y]>map[x][y])
{
ans = dfs(new_x,new_y);
maxn = max(ans,maxn);
}
}
return dp[x][y] = maxn+map[x][y];
}
} int main()
{
while(scanf("%d%d",&n,&k))
{
if(n==- && k==-) break;
for(int i = ;i < n;i++)
for(int j = ;j <n;j++)
scanf("%d",&map[i][j]);
memset(dp,-,sizeof(dp));
printf("%d\n",dfs(,));
}
return ;
}

HDU-1078的更多相关文章

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

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

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

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

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

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

  4. hdu 1078(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 //dp[i][j]表示从点i,j处开始能获得的最多cheese #include <io ...

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

    题目大意:   给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值   (题目很容 ...

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

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

  7. hdu 1078 FatMouse and Cheese【dp】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:每次仅仅能走 横着或竖着的 1~k 个格子.求最多能吃到的奶酪. 代码: #include ...

  8. hdu 1078 FatMouse and Cheese(简单记忆化搜索)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 一道 ...

  9. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  10. hdu 1078 FatMouse and Cheese

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

随机推荐

  1. android Json解析详解

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语 言的支持),从而可以在不同平台间进行数 ...

  2. 获取CPU使用情况信息(转)

    获取了内存使用情况,也可以使用PHP的 getrusage()获取CPU使用情况,该方法在windows下不可用.    print_r(getrusage()); /* 输出 Array ( [ru ...

  3. java相关的路径获取 (转载 http://tomfish88.iteye.com/blog/971255)

    在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...

  4. 使用truncate命令清空当前用户所有表的所有数据

    --批量清空当前用户所有表的所有数据 declarev_sql varchar2(2000) ;CURSOR cur is select table_name from user_tables ord ...

  5. Freemarker常用技巧(二)

    1 list.break指令<#list sequence as item>  ...</#list>tem_index:当前变量的索引值.item_has_next:是否存在 ...

  6. 富文本 Htmll类 html标签

    HTML类可解析的标签 在手机上显示从网络端获取的数据有两种方式,一种是WebView,另一种是TextView,WebView大家都知道,功能强大但不灵活,下面主要说下TextView. 通过查看a ...

  7. hdu 1870

    水题.... AC代码: #include <iostream> #include <queue> using namespace std; int main() { char ...

  8. .net中Web.config文件的基本原理及相关设置

    11.7  使用web.config配置文件 Web配置文件web.config是Web 应用程序的数据设定文件,它是一份 XML 文件,内含 Web 应用程序相关设定的 XML 标记,可以用来简化  ...

  9. 第一次启动MySQL时报错

    [root@localhost~]#/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mys ...

  10. UI基本之UITextField相关方法属性

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(, , , )]; // ...