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. SQL SERVER 2000 遍历父子关系数据的表(二叉树)获得所有子节点 所有父节点及节点层数函数

    ---SQL SERVER 2000 遍历父子关系數據表(二叉树)获得所有子节点 所有父节点及节点层数函数---Geovin Du 涂聚文--建立測試環境Create Table GeovinDu([ ...

  2. RGB888转RGB666

    内存中的数据排列高位在左,低位在右 RGB888->RGB666 高 -------低 B[3]         B[2]          B[1]         B[0]         ...

  3. 【贪心】 poj 1032 和为n的若干数最大乘积

    给出n,把n分解为若干不相同数之和,使之乘积最大.贪心,Discuss里面的思路:把n分解为从2开始的连续整数,如果有多,则从高位开始依次加1.如26,我们得到2+3+4+5+6,此时还剩余6(26- ...

  4. java解析xml文件并输出

    使用java解析xml文件,通过dom4j,代码运行前需先导入dom4j架包. ParseXml类代码如下: import java.io.File; import java.util.ArrayLi ...

  5. java数据结构之有序表查找

    这篇文章是关于有序表的查找,主要包括了顺序查找的优化用法.折半查找.插值查找.斐波那契查找: 顺序优化查找:效率极为底下,但是算法简单,适用于小型数据查找: 折半查找:又称为二分查找,它是从查找表的中 ...

  6. 《JavaScript高级程序设计》读书笔记 ---变量

    ECMAScript 的变量是松散类型的,所谓松散类型就是可以用来保存任何类型的数据.换句话说,每个变量仅仅是一个用于保存值的占位符而已.定义变量时要使用var 操作符(注意var 是一个关键字),后 ...

  7. hdu 2647 Reward(拓扑排序,反着来)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  8. PHP AJAX技术

    AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX 通过在后台与服务器进行少量数据交换,使网页实现异步更新.这意味着可以在不重载整个页面的情况下,对网页的某些部分进行更 ...

  9. Linux学习 -- 软件包管理

    1 软件包类型 源码包 脚本安装包  install.sh  不常用 二进制包(rpm包.系统默认包) RedHat -- rpm包 Debian,Ubuntu -- beb包 2 RPM包命令管理 ...

  10. android 在代码中使用 #ffffff 模式 设置背景色

    differentsum.setBackgroundColor(Color.parseColor("#F3733F"));