【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

题干:

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

 

题目大意:

有一种游戏是的玩法是这样的:
有一个n*n的格子,每个格子有一个数字。
遵循以下规则:
1. 玩家每次可以由所在格子向上下左右四个方向进行直线移动,每次移动的距离不得超过m
2. 玩家一开始在第一行第一列,并且已经获得该格子的分值
3. 玩家获得每一次移动到的格子的分值
4. 玩家下一次移动到达的格子的分值要比当前玩家所在的格子的分值要大。
5. 游戏所有数字加起来也不大,保证所有数字的和不会超过int型整数的范围
6. 玩家仅能在n*n的格子内移动,超出格子边界属于非法操作
7. 当玩家不能再次移动时,游戏结束
现在问你,玩家所能获得的最大得分是多少?

输入是n和m。

 

解题报告:

     水题,,但是刚开始读错题,没看到只能从(1,1)点开始游戏 这个条件,所以WA了、、

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
ll dp[105][105];
int a[105][105];int n,m;
bool ok(int x,int y) {if(x>=1&&x<=n&&y>=1&&y<=n) return 1;else return 0;
}
ll dfs(int x,int y) {if(dp[x][y] != -1) return dp[x][y];ll res = 0;int tx,ty;for(int k = 0; k<4; k++) {tx = x;ty = y;for(int i = 1; i<=m; i++) {tx += nx[k];ty += ny[k];if(ok(tx,ty)==0) continue;if(a[tx][ty] <= a[x][y]) continue;res = max(res,dfs(tx,ty));}}return dp[x][y]=res+a[x][y];}
int main()
{while(~scanf("%d%d",&n,&m)) {if(n==-1 && m==-1) break;memset(dp,-1,sizeof dp);ll ans = 0;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&a[i][j]);}}		
//		for(int i = 1; i<=n; i++) {
//			for(int j = 1; j<=n; j++) {
//				ans = max(ans,dfs(i,j));
//			}
//		}ans = dfs(1,1);printf("%lld\n",ans);} return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441080.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

获取excel名称java_使用Apache POI获取大型Excel文件的Excel工作表名称

小编典典为了显示Gagravarr的评论可能意味着什么&#xff1a;该XSSFReader包含方法XSSFReader.getSheetsData其中“返回一个迭代器&#xff0c;这将让你在把所有的不同的表&#xff0c;每个表的InputStream中只有打开时开始迭代器牵强。这是给你的时候&#xff0c;每个做关闭In…

【CodeForces - 298C】Parity Game (思维,有坑)

题干&#xff1a; You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and &q…

java求小数高精度_浅谈Java中的高精度整数和高精度小数

在实际编码中&#xff0c;会遇到很多高精度的事例&#xff0c;比如&#xff0c;在计算金钱的时候就需要保留高精度小数&#xff0c;这样计算才不会有太大误差&#xff1a;在下面的代码中&#xff0c;我们验证了&#xff0c;当两个float型的数字相加&#xff0c;得到的结果和我们…

【CodeForces - 298B 】Sail (模拟,题意)

题干&#xff1a; The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently…

java中能构成循环的语句_《编程导论(Java)#183;3.2.4 循环语句》

本文全然复制《编程导论(Java)3.2.4 循环语句》的内容。除【】中的说明文字。请阅读和比較其它编程教材。我知道。假设我是一个刚開始学习的人&#xff0c;《编程导论(Java)》非常不适合自学。建议同学们阅读时&#xff0c;一定选择一本其它的书同一时候看&#xff0c;或上网。…

【CodeForces - 340B 】Maximal Area Quadrilateral (计算几何,枚举,有坑)

题干&#xff1a; Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners)…

php网站的编辑器,5款适合PHP使用的HTML编辑器推荐

概述Web开发中&#xff0c;很多地方都会用到HTML编辑器(富文本编辑器)&#xff0c;我也用过几种&#xff0c;如UEditor、CkEditor等。这几天看了几篇文章&#xff0c;都是关于HTML编辑器的&#xff0c;写个文章记录下。推荐的编辑器simditor这个编辑器是前几天刚看到的&#xf…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级 )】小乐乐和25(模拟,技巧)

题干&#xff1a; 小乐乐特别喜欢25这个数字&#xff0c;他想把所有的数字都变成25的倍数。 现在小乐乐得到一个数字&#xff0c;想问问你最少用几次操作才可以把这个数字改造成25的倍数。 对于一次操作我们可以把相邻的两位做交换&#xff0c;比如123经过一次操作之后就可以…

怎么表示一个PHP语句块,php switch语句多个值匹配同一代码块应用示例

先说说switch()语句的格式switch(表达式){case 匹配1&#xff1a;当匹配1和表达式匹配成功执行的代码;break;case 匹配2&#xff1a;当匹配2和表达式匹配成功执行的代码;break;default&#xff1a;如果case语句没有与表达式成功所执行的代码;}理解 switch 是怎样执行的非常重要…

php查找顶级分类,php 无限级分类 获取顶级分类ID,php顶级_PHP教程

php 无限级分类 获取顶级分类ID&#xff0c;php顶级有这样一个表&#xff0c;id是分类的ID&#xff0c;name是分类名称&#xff0c;pid是上级分类的ID。现在有个分类ID&#xff0c;程序要找到它上级的上级的上级……分类的ID&#xff0c;简单说就是找出顶级分类的ID。比如“新鲜…

【POJ - 2392】Space Elevator (dp,优秀的背包问题)

题干&#xff1a; The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 < K < 400) different types of blocks with which to build the tower. Each block of type i has heigh…

php三种web开发技术,三种WEB开发主流技术ASP-PHP-JSP的评价

三种Web开发主流技术ASP-PHP-JSP的评价[摘要]本文从程序开发者的角度&#xff0c;对现今社会盛行的WEB开发技术进行了分析&#xff0c;旨在让WEB 开发人员、使用者、准备学习的人对WEB开发技术有更加深入的了解&#xff0c;并为WEB应用程序开发提供指导。[关键词]WEB开发技术 A…

*【CodeForces - 1088 ABC】套题比赛,A水题B模拟C构造D交互

A. Input The only line contains the integer xx (1≤x≤100)(1≤x≤100). Output You should output two integers aa and bb, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (wit…

php 计算前几天,php计算几分钟前、几小时前、几天前的几个函数、类分享

搜索热词一、函数实现实例1&#xff1a;代码如下:function time_tran($the_time){$now_time date("Y-m-d H:i:s",time()8*60*60);$now_time strtotime($now_time);$show_time strtotime($the_time);$dur $now_time - $show_time;if($dur < 0){return $the_tim…

【CodeForces - 608D】Zuma(区间dp)

题干&#xff1a; Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible. In one second, Geno…

matlab没有int函数,matlab 未定义与 'char' 类型的输入参数相对应的函数 'int'。

最后你那zd句plot(int(k),double(s));里的int(k);不对。你如果想以k为横坐标&#xff0c;直接把int去掉&#xff0c;如回果非想要变成整数可以用floor(k);注&#xff1a;fix&#xff1a;向零取整 floor&#xff1a;向小取整 round&#xff1a;四舍五入 ceil&#xff1a;向...如…

【CodeForces - 347C 】Alice and Bob (思维,数学,等差数列)

题干&#xff1a; It is so boring in the summer holiday, isnt it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each …

java读取yaml配置文件,snakeyaml读取yaml配置文件

下面上我解析yaml文件的例子&#xff1a;// me.yamlage: 1name: jiaobuchongparams:event: whats upurl: http://www.jiaobuchong.comfavoriteBooks:- Gone with the wind- The Little Prince对应的JavaBean类&#xff1a;// Me.javapublic class Me {private Integer age;priv…

算法讲解 -- 区间dp经典模型与优化(石子归并)

石子合并问题是最经典的DP问题。首先它有如下3种题型&#xff1a; PPT讲解&#xff1a;点击打开链接 (1)有N堆石子&#xff0c;现要将石子有序的合并成一堆&#xff0c;规定如下&#xff1a;每次只能移动任意的2堆石子合并&#xff0c;合并花费为新合成的一堆石子的数量。求将…

cpu影响matlab仿真速度吗,Proteus仿真速度很慢的分析

类型&#xff1a;行业软件大小&#xff1a;1.1M语言&#xff1a;中文 评分&#xff1a;6.5标签&#xff1a;立即下载这篇文章是我的个人实践经验&#xff1a;很多朋友在做Proteus硬件仿真的时候可能都碰上了仿真速度慢的问题&#xff0c;在点击了开始仿真之后&#xff0c;CPU过…