FatMouse and Cheese

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

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 <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
const int maxn = ;
int dp[maxn][maxn], a[maxn][maxn];
int des[][] = {-, , , , , , , -};//4个方向
int n, k;

bool check(int x, int y) {//越界
if(x <  || x >= n || y <  || y >= n)
return false;
return true;
}

int dfs(int x, int y) {
int ans = ;//记录最大值
if(dp[x][y] == ) {
for(int i = ; i <= k; i++) {//k步
for(int j = ; j < ; j++) {//4个方向
int newx = x + des[j][] * i;//走k步！！太酷了
int newy = y + des[j][] * i;
if(check(newx, newy)) {
if(a[newx][newy] > a[x][y])
ans = max(ans, dfs(newx, newy));//最大值
}
}
}
dp[x][y] = ans + a[x][y];//更新dp[x][y]
}
return dp[x][y];
}

int main() {
while(~scanf("%d%d", &n, &k)) {
if(n == -)
break;
mem(dp, );
for(int i = ; i < n; i++) {
for(int j = ; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
cout << dfs(, ) << endl;//dfs
}
}```

## kuangbin专题十二 HDU1078 FatMouse and Cheese ）(dp + dfs 记忆化搜索)的更多相关文章

1. hdu 1078 FatMouse and Cheese （dfs+记忆化搜索）

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

2. kuangbin专题十二 POJ1661 Help Jimmy （dp）

Help Jimmy Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14214   Accepted: 4729 Descr ...

3. kuangbin专题十二 HDU1176 免费馅饼 （dp）

免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

4. kuangbin专题十二 HDU1069 Monkey and Banana (dp)

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

5. hdu 1078 FatMouse and Cheese（简单记忆化搜索）

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

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

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

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. kuangbin专题十二 POJ3186 Treats for the Cows （区间dp）

Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7949   Accepted: 42 ...

9. kuangbin专题十二 HDU1029 Ignatius and the Princess IV （水题）

Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

## 随机推荐

1. 一款开源免费的.NET文档操作组件DocX（.NET组件介绍之一）

在目前的软件项目中,都会较多的使用到对文档的操作,用于记录和统计相关业务信息.由于系统自身提供了对文档的相关操作,所以在一定程度上极大的简化了软件使用者的工作量. 在.NET项目中如果用户提出了相关文 ...

2. OOP复习笔记

/*OOP相关的代名词不做讲解*/ OOP的三大特征: 封装 - 继承 - 多态 -----------------------------------目录---------------------- ...

3. Ember Charts – 基于 Ember &amp; D3 的图表库

Ember Charts 是一个基于 Ember.js 和 D3.js 的图表库.它包括时间序列.柱状图.饼图.点图,很容易扩展和修改.这些图表组件代表图表交互性和演示的最佳实践,是高度可定制和可扩展 ...

4. WebRTC手记之本地视频采集

转载请注明出处:http://www.cnblogs.com/fangkm/p/4374610.html 前面两篇文章介绍WebRTC的运行流程和使用框架接口,接下来就开始分析本地音视频的采集流程.由 ...

5. java io读书笔记（1）综述

学习,是要持之以恒的,再读一本书,坚持. Java™ I/O, 2nd Edition By Elliotte Rusty Harold ............................... ...

6. LLVM language 参考手册（译）（5）

垃圾回收器名称(Garbage Collector Names) 每一个函数可以制定一个垃圾回收期的名称,这个名称是一个简单的字符串: define void @f() gc "name&q ...

7. mysql 开发进阶篇系列 46 物理备份与恢复( xtrabackup的 选项说明，增加备份用户，完全备份案例)

一. xtrabackup 选项说明 在操作xtrabackup备份与恢复之前,先看下该工具的选项,下面记录了xtrabackup二进制文件的部分命令行选项,后期把常用的选项在补上.点击查看xtrab ...

8. 一颗树下的input框超出的部分打点鼠标移动显示

9. JS图片水印