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. crtmpserver 基本流程分析

    近期在研究crtmpserver,这里记录下学习过程,首先我们先分析下基本流程. 1.初始化流程 InitNetworking---初始化网络 Initialize Logger::Init()--- ...

  2. 分层模型的典型应用和FishiGUI的分层模型

    分层模式的典型应用: 对于交互类型的软件也能够採用分层模式来进行架构分析,一般来说将交互性的软件分为三个层次比較合适:显示层的职责是为了显示信息,应用逻辑层封装那些一般不easy发生变化的核心逻辑,而 ...

  3. fork 和 vfork 的区别与联系

    vfork用于创建一个新进程,而该新进程的目的是exec一个新进程,vfork和fork一样都创建一个子进程,但是它并不将父进程的地址空间完全复制到子进程中,不会复制页表.因为子进程会立即调用exec ...

  4. 【iOS】Resumable Doanloads(断点下载)

    这里我们只讨论iOS平台下的通用app,我们可以自己写代码来实现resume downloads,解释如下. resume一个HTTP下载不难,但必须要理解一些关键的HTTP概念: entity ta ...

  5. 20M宽带的网速等价于多少?

    最近有朋友问我:我家的宽带是20兆的,怎么网速这么慢? 运营商说的20M,完整的单位应该是20Mbps(bps:比特率),而日常中所说的下载速度单位是MB,两者是不一样的. 它们之间的换算关系是:1M ...

  6. Entity Framework Code First 多数据库 控制台迁移代码

    1.启动迁移 Enable Migrations Enable-Migrations -MigrationsDirectory "MigrationsOne" -ContextTy ...

  7. HDU3756

    题意:给定三围空间里面某些点,求构造出一个棱锥,将所有点包含,并且棱锥的体积最小. 输入: T(测试数据组数) n(给定点的个数) a,b,c(对应xyz坐标值) . . . 输出: H(构造棱锥的高 ...

  8. HTML基础总结<标题>

      HTML: 标题 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. <h1> 定义最大的标题.<h6> 定义最小的标题. & ...

  9. AppDomain.CurrentDomain.GetAssemblies()

    AppDomain.CurrentDomain.GetAssemblies() ,获取已加载到此应用程序域的执行上下文中的程序集 解释地址 从微软的解释也可以得知,这个方法只能获取已经加载到此应用程序 ...

  10. activiti总结

    1.activiti如何修改登录用户名?在哪个数据库里面添加. 2.activiti的启动和部署在http://activiti.org/userguide/index.html#demo.setup ...