FatMouse and Cheese HDU - 1078(记忆化搜索入门模板)

题意:

n * n的正方形格子(每个格子均放了奶酪),老鼠从(0,0)开始,每次最多移动k步,可以选择上下左右四个方向移动,下一个移动点奶酪块数量必须要大于当前点。

整理模板ing…

题目:

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

分析:

记忆化搜索入门,当采用记忆化搜索时,不必事先确定各状态的计算顺序,但需要记录每个状态“是否已经计算过”。
总结一下记忆化搜索是啥:
1.不依赖任何 外部变量(调用函数内的变量也没有变化)
2.答案以返回值的形式存在, 而不能以参数的形式存在(就是不能将 dfs 定义成 dfs(pos ,tleft , nowans ) , 这里面的 nowans 不符合要求).
3.对于相同一组参数, dfs 返回值总是相同的(参见第一条)
就这道题复杂度为:O(n2n^{2}n2):每个点只遍历了一次,从O(2n2^{n}2n)~O(n2n^{2}n2)这是记忆化搜索算法在时间复杂度上的优越性

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m;
int dp[110][110],a[110][110];
int c[4][2]= {0,1,0,-1,1,0,-1,0};
int dfs(int u,int v)
{if(dp[u][v]!=-1)return dp[u][v];dp[u][v]=a[u][v];for(int i=0; i<4; i++)for(int j=1; j<=m; j++){int x=u+c[i][0]*j;int y=v+c[i][1]*j;if(x<0||y<0||x>=n||y>=n||a[u][v]>=a[x][y])continue;dp[u][v]=max(dp[u][v],dfs(x,y)+a[u][v]);}return  dp[u][v];
}
int main()
{while(~scanf("%d%d",&n,&m)){if(n==-1&&m==-1)break;for(int i=0; i<n; i++)for(int j=0; j<n; j++)scanf("%d",&a[i][j]);memset(dp,-1,sizeof(dp));printf("%d\n",dfs(0,0));}return 0;
}

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

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

相关文章

.NET 程序下锐浪报表 (Grid++ Report) 的绿色发布指南

在锐浪报表官方为 CSharp 编写的开发文档&#xff1a;“在C#与VB.NET中开始使用说明.txt” 中&#xff0c;关于发布项目是这么描述的&#xff1a;★发布你的项目&#xff0c;用VS.NET制作安装程序&#xff1a;1、先创建安装项目&#xff1a;在解决方案资源管理器的根节点上点右…

C++输出对齐(如何使输出对齐)

代码如下: #include <iostream> #include <iomanip> using namespace std;int main() {cout << setw(15)<<std::left<<"unsigned int" << setw(15) << std::left<<sizeof(unsigned) << endl;cout <<…

HDFS(一)

HDFS&#xff08;一&#xff09; 参考&#xff1a; http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_design.html https://www.cnblogs.com/zsql/p/11587240.html Hadoop Distribute File System&#xff1a;Hadoop分布式文件系统&#xff0c;Hadoop核心组件之一&#xff0c;作为…

Silver Cow Party POJ - 3268(dijkstra+反向交换)

题意&#xff1a; 求X到某点来回路程的最短路的最大值。 模板更新中。。。 题目 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1…N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,00…

[开源] .Net orm FreeSql 1.5.0 最新版本(番号:好久不见)

废话开头这篇文章是我有史以来编辑最长时间的&#xff0c;历时 4小时&#xff01;&#xff01;&#xff01;原本我可以利用这 4小时编写一堆胶水代码&#xff0c;真心希望善良的您点个赞&#xff0c;谢谢了&#xff01;&#xff01;很久很久没有写文章了&#xff0c;上一次还是…

MapReduce简述

MapReduce 参考&#xff1a; https://www.cnblogs.com/lixiansheng/p/8942370.html https://baike.baidu.com/item/MapReduce/133425?fraladdin 概念 MapReduce是面向大数据并行处理的计算模型&#xff0c;用于大规模数据集的并行计算。它提供了一个庞大但设计精良的并行计算…

调试实战 —— dll 加载失败之全局变量初始化篇

前言 最近项目里总是遇到 dll 加载不上的问题&#xff0c;原因各种各样。今天先总结一个虽然不是项目中实际遇到的问题&#xff0c;但是却非常经典的问题。其它几种问题&#xff0c;后续慢慢总结。示例代码包含一个 exe 工程&#xff0c;两个 dll 工程。exe 会加载两个 dll 并调…

ROADS POJ - 1724(最短路+邻接表+dfs)

题意&#xff1a; N个城市&#xff0c;编号1到N。城市间有R条单向道路。有长度和过路费两个属性。Bob只有K块钱&#xff0c;他想从城市1走到城市N。问最短共需要走多长的路。如果到不了N&#xff0c;输出-1。 题目&#xff1a; N cities named with numbers 1 … N are conn…

MongoDB副本集

参考&#xff1a;https://www.cnblogs.com/littleatp/p/8562842.html https://www.cnblogs.com/ilifeilong/p/14347008.html MongoDB副本集 MongoDB副本集是由一组Mongod实例&#xff08;进程&#xff09;组成&#xff0c;包含一个Primary节点和多个Secondary节点。客户端的所…

博客系统知多少:揭秘那些不为人知的学问(一)

点击上方蓝字关注“汪宇杰博客”导语在我们生活的年代&#xff0c;博客并不稀奇&#xff0c;甚至可以说是随处可见。从最早的搜狐、新浪博客&#xff0c;再到每个人都曾记录青春的 QQ 空间&#xff0c;再到现在的 Vlog 与 Plog&#xff0c;似乎拥有一个自己的博客并不是什么难事…

MongoDB 分片

MongoDB 分片 高数据量&#xff08;消耗内存&#xff09;和高吞吐量&#xff08;消耗CPU&#xff09;的数据库应用会对单机的性能造成较大压力&#xff0c;为了解决这些问题&#xff0c;一般采用两种方法&#xff1a;水平扩展&#xff08;将数据集分布在多个服务器上&#xff…

How many ways HDU - 1978(记忆化搜索关于求多少种方式模板)

题目&#xff1a; 这是一个简单的生存游戏&#xff0c;你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m)。游戏的规则描述如下&#xff1a; 1.机器人一开始在棋盘的起始点并有起始点所标有的能量。 2.机器人只能向右或者向下走&#xff0c;并且每走一步消耗一单位…

Sql Server之旅——第七站 复合索引和include索引到底有多大区别?

索引和锁&#xff0c;这两个主题对我们开发工程师来说&#xff0c;非常的重要。。。只有理解了这两个主题&#xff0c;我们才能写出高质量的sql语句&#xff0c;在之前的博客中&#xff0c;我所说的索引都是单列索引。。。当然数据库不可能只认单列索引&#xff0c;还有我这篇的…

Go中new和make的区别

Go中new和make的区别 变量声明 当我们声明变量时可以使用var关键字&#xff0c;当不指定变量的默认值时&#xff0c;这些变量的默认值就是他们的零值&#xff0c;比如int的默认值为0&#xff0c;string的默认值为""&#xff0c;引用类型的零值为nil。 但是当我们在…

POJ 3159 Candies(差分约束+SPAF)

题意&#xff1a; 给n个小朋友分发糖果&#xff0c;但小朋友们之间有嫉妒心。接下来m行&#xff0c;每行三个数&#xff0c;分别表示小朋友A希望B得到的糖果不能比他多x个。要求你计算在满足所有小朋友的条件的情况下最多需要准备多少颗糖。 题目&#xff1a; During the ki…

掌握了Docker Layer Caching才敢自称精通Dockerfile

长话短说&#xff1a;本次原创将向您展示在Docker中使用Layer Cache以加快镜像构建。“这个话题的初衷在于&#xff1a;应用打包过程是很慢的(下载并安装框架&第三方依赖包、生成assets)&#xff0c;这个过程在Docker中也不能避免。About Layer Caching in DockerDocker使…