FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7576    Accepted Submission(s): 3133

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
 动态化搜索,详见代码。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
int n,k;
const int maxn = ;
int maze[maxn][maxn],dp[maxn][maxn];
int dfs(int p,int q){
if(dp[p][q]) return dp[p][q];
int l,r,d,u;
int ans = ,maxx = ;
l = p - k;
r = p + k;
if(l<) l = ;
if(r>=n) r = n-;
for(int i = l; i<=r; i++){
if(maze[i][q]>maze[p][q]){
ans = dfs(i,q);
if(ans>maxx) maxx = ans;
}
}
u = q + k;
d = q - k;
if(d<) d = ;
if(u>=n) u = n-;
for(int i = d; i<=u; i++){
if(maze[p][i]>maze[p][q]){
ans = dfs(p,i);
if(ans>maxx) maxx = ans;
}
}
dp[p][q] = maxx + maze[p][q];
return dp[p][q];
}
void input(){
while(scanf("%d%d",&n,&k)!=EOF&&n != -&&k != -){
for(int i = ; i<n; i++)
for(int j = ; j<n; j++)
scanf("%d",&maze[i][j]);
memset(dp,,sizeof(dp));
printf("%d\n",dfs(,));
}
}
int main()
{
input();
return ;
}

卷珠帘

FatMouse and Cheese 动态化搜索的更多相关文章

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

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

  2. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

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

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

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

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

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

  5. HDU1078 FatMouse and Cheese 【内存搜索】

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

  6. FatMouse and Cheese

    FatMouse and Cheese Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  7. HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏

    FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...

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

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

  9. 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 ...

随机推荐

  1. hdu_5044_Tree(树链剖分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5044 题意:给一棵树,在点和边上操作 题解:树链剖分,剖完后用树状数组维护即可,因为只有加减操作,连树 ...

  2. python常用操作

    1.进入python命令行: #python >>>... 退出python命令行 >>>exit() #... 2.运行1.py 直接运行1.py: #pytho ...

  3. 今天开通博客,记录我作为一个小菜鸟在iOS学习中的点点滴滴

    一直以来都是默默的关注各位同仁,没有为网站作什么贡献. 现在借开始学习iOS开发的这个机会开博,集中于介绍这个过程,激励我自己. 谢谢大家!

  4. 远程连接(ssh安装)

    更新源列表打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--& ...

  5. 【重大bug】viewpager使用的时候加载数据应该在setOnPageChangeListener里加载

    [重大bug]viewpager使用的时候加载数据应该在setOnPageChangeListener里的onPageSelected里加载,我说怎么首页有数据,第二页就是空白,就是加载了但是数据不显 ...

  6. 数论+dp Codeforces Beta Round #2 B

    http://codeforces.com/contest/2/problem/B 题目大意:给你一个n*n的矩形,问从(1,1)出发到(n,n),把图中经过的所有的数字都乘在一起,最后这个数字有多少 ...

  7. 依赖注入(DI)和控制反转(IOC)【回顾】

    在java开发中广泛的使用了IOC的思想,在PHP中同样也在广泛使用. interface Coder { public function coding(); } 实现类Javaer class Ja ...

  8. asp.net如何删除文件夹及文件内容操作

    static void DeleteDirectory(string dir) { && Directory.GetFiles(dir).Length == ) { Directory ...

  9. 转 linux下xargs命令用法详解

    xargs在linux中是个很有用的命令,它经常和其他命令组合起来使用,非常的灵活. xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤 ...

  10. php过滤HTML标签、属性等正则表达式汇总

    $str=preg_replace("/\s+/", " ", $str); //过滤多余回车 $str=preg_replace("/<[ ] ...