kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
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
1 2 5
10 11 6
12 12 7
-1 -1
Sample Output
37
题目大意:和滑雪比较类似,只是多了一个最多k步的限制。dp + dfs即可
记忆化搜索。dfs一个点,求k步之内的最大值。 还是对搜索发怵!!!!
#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+;
//head
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 记忆化搜索)的更多相关文章
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- kuangbin专题十二 POJ1661 Help Jimmy (dp)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14214 Accepted: 4729 Descr ...
- kuangbin专题十二 HDU1176 免费馅饼 (dp)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- kuangbin专题十二 HDU1069 Monkey and Banana (dp)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1078 FatMouse and Cheese(简单记忆化搜索)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 一道 ...
- HDU 1078 FatMouse and Cheese ( DP, DFS)
HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...
- 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 ...
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
随机推荐
- 一次Mono解析Excel文档编码出错排查记录
最近在捯饬Asp.Net站点部署到Linux平台上面,在文档导入的操作中经过网上搜索采用了能够支持跨平台的ExcelDataReader组建.在本地windows上测试通过NuGet安装的组建,这货依 ...
- 阿里云服务器ubuntu安装redis2.8.13
阿里云服务器ubuntu安装redis2.8.13 2014-09-04 16:14 | coding云 | 2198次阅读 | 暂无评论 一.下载redis 可以先下载到本地,然后ftp到服 ...
- GCD详细用法
一.延迟执行 1.介绍 第一种方法,该方法在那个线程调用,那么run就在哪个线程执行(当前线程),通常是主线程. [self performSelector:@selector(run) withOb ...
- JAVA基础知识总结5(面向对象特征之一:继承)
继 承: 1:提高了代码的复用性. 2:让类与类之间产生了关系,提供了另一个特征多态的前提. 父类的由来:其实是由多个类不断向上抽取共性内容而来的. JAVA只支持单继承.java虽然不直接支持多继承 ...
- sqlplus--spool命令参数详解
sqlplus--SPOOL参数详解 Spool是Oracle快速导出数据的工具,是sqlplus的指令,不是sql语法里的东西 一.Spool常用的设置set arraysize 5000; // ...
- POJ 1191 棋盘分割 (区间DP,记忆化搜索)
题面 思路:分析公式,我们可以发现平均值那一项和我们怎么分的具体方案无关,影响答案的是每个矩阵的矩阵和的平方,由于数据很小,我们可以预处理出每个矩阵的和的平方,执行状态转移. 设dp[l1][r1][ ...
- C++用指针变量作为函数的参数接受数组的值的问题的总结
实参和形参的四种结合方式 实参 形参 实例 数组名 数组名 1.1 数组名 指针变量 1.2 指针变量 数组名 1.3 指针变量 指针变量 1.4 本文以输入10个整数,然后对其进行排序,然后输出的程 ...
- Angular04 组件动态地从外部接收值、在组件中使用组件
一.组件从外部接收值 1 修改组件的ts文件,让组件的属性可以从外部接收值 1.1 导入Input注解对象 1.2 在属性变量前面添加 @Input() 注解 1.3 去掉构造器中的属性变量赋值语句 ...
- win10获取超级管理员权限脚本实现
建立一个TXT文件,把下面的脚本贴到里面,然后把后缀改成reg格式,双击添加到注册表就可以了, win10_1703版本亲测可用.... Windows Registry Editor Version ...
- 在Linux里安装jdk
一.系统环境说明: [操作系统]:Ubuntu 18.04.1 Desktop [JDK]:jdk1.8.0_181,文件名称:jdk-8u181-linux-x64.tar 二.准备jdk文件 下载 ...