Description

Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子

 1  2  3  4 5 
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

Input

输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。

Output

输出最长区域的长度。

Sample Input

5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

Sample Output

25

记忆化搜索当然也还是搜索,只不过在普通的搜索上做了一些优化,因为使用的是动态规划的方法进行的优化,使得它看起来更像DP。
首先考虑如果这道题我们只是用普通搜索的方法做,那么就是从某一点开始搜索,一直搜索到最低点,不可以扩展的地方,记录下下滑的长度,那个某一点当然是不可预知的,所以得枚举,也就是说图中的每一点都要搜上一遍,请注意图最大是100*100的,超时肯定是不必说的 所以可以考虑使用dp做,dp比递归好的一个地方在于它解决了重叠子问题,这道题存在很多重叠子问题是不必说的,例如我从10出发,搜到了一条路,下一次我从25出发,等我到达10这一点时,虽然我以前走过这一条路,可是并没有把这一条路记录下来,所以还得重新再走上一遍,这样的重叠问题越多,时间浪费也就越严重,这样的浪费导致了指数级的时间复杂度,是很恐怖的 所以可以使用book【i】【j】(book在英文中有标记的意思)表示从第i行j个开始滑所能下滑的高度 那么动态规划方程为dp(i,j)=max{dp(i-1,j),dp(i+1,j),dp(i,j-1),dp(i,j+1),这些点的高度必须小于i行j列这个点的高度,否则不予考虑}+1
#include"iostream"
#include"cstring"
using namespace std; const int maxn=; int m,n,book[maxn][maxn],a[maxn][maxn];
int dir[][]={{-,},{,},{,},{,-}}; void Init()
{
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
cin>>a[i][j];
memset(book,-,sizeof(book));
} int dp(int i, int j)
{
int k, ni, nj;
if (book[i][j] > ) return book[i][j];
book[i][j] = ;
for (k = ; k < ; ++k)
{
ni = i + dir[k][];
nj = j + dir[k][];
if(ni>=&&ni<=m&&nj>=&&nj<=n&&a[i][j]>a[ni][nj]&&dp(ni,nj)+>book[i][j])
book[i][j] = book[ni][nj] + ;
}
return book[i][j];
} void Work()
{
int ans=-;
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
if(dp(i,j)>ans) ans=book[i][j];
cout<<ans<<endl;
// dp(3,3);
/* for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++) cout<<book[i][j]<<" ";
cout<<endl;
}*/
} int main()
{
while(cin>>m>>n)
{
Init();
Work();
}
return ;
}

HOLLO


集训第五周动态规划 I题 记忆化搜索的更多相关文章

  1. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  2. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  3. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  4. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  5. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  6. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  7. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  9. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

随机推荐

  1. Luogu P2458 [SDOI2006]保安站岗【树形Dp】

    题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...

  2. #define及其用法

    #define     在#define中,标准只定义了#和##两种操作.#用来把参数转换成字符串,##则用来连接前后两个参数,把它们变成一个字符     串. #include<stdio.h ...

  3. 机器学习基础算法__python实现(基于numpy等基础库)

    # 博客转自https://blog.csdn.net/weixin_39561100/article/details/80879211 主要是将<机器学习实战>中的算法实现一遍,后续会慢 ...

  4. 51nod 1154 回文串划分

    1154 回文串划分 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有 ...

  5. Retinex系列之McCann99 Retinex 分类: 图像处理 Matlab 2014-12-03 11:27 585人阅读 评论(0) 收藏

    一.McCann99 Retinex McCann99利用金字塔模型建立对图像的多分辨率描述,自顶向下逐层迭代,提高增强效率.对输入图像的长宽有 严格的限制,要求可表示成 ,且 ,. 上述限制来源于金 ...

  6. VS2010下安装Opencv 分类: Opencv 2014-11-02 13:51 778人阅读 评论(0) 收藏

    Opencv作为一种跨平台计算机视觉库,在图像处理领域得到广泛的应用,下面介绍如何在VS2010中安装配置Opencv 一.下载Opencv 下载网址:http://sourceforge.net/p ...

  7. ATM机(非函数版)

    #include<stdio.h>#include<stdlib.h>int main(void){char zhangHao[]="123";int mi ...

  8. C# .net实现下载,带进度条

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. 使用 Java 发送邮件

    在我们的应用程序中有时需要给用户发送邮件,例如激活邮件.通知邮件等等.那么如何使用 Java 来给用户发送邮件呢? 使用 java 代码发送邮件 使用工具类发送邮件 使用Spring进行整合发送邮件 ...

  10. 构建微服务开发环境5————安装Node.js

    [内容指引] 下载Node.js: Mac下安装Node.js: Windows下安装Node.js; 查看node和npm的版本. 一.下载Node.js 访问Node.js官网:https://n ...