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. 查找MySQL和 SQL sever data

    MySql SQL server

  2. C语言经典程序1

    //打印2-200的所有素数 (除了1和它本身,不能被小于它的其它数整除的数称为素数) ; ;i<;i++) //i遍历2-200 { ; //先默认这个数为素数 ;j<i-;j++) / ...

  3. 解题报告:hdu 1073 Online Judge

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1073 Problem Description Ignatius is building an Onli ...

  4. magento CURD操作

    查询: $model = Mage::getModel('mynews/mynews'); $collection = $model->getCollection(); $collection- ...

  5. magento controller直接渲染Block 以及传参

    class Jago_Deal_IndexController extends Mage_Core_Controller_Front_Action { public function ajaxActi ...

  6. Java多线程学习---------超详细总结(java 多线程 同步 数据传递 )

    目录(?)[-] 一扩展javalangThread类 二实现javalangRunnable接口 三Thread和Runnable的区别 四线程状态转换 五线程调度 六常用函数说明 使用方式 为什么 ...

  7. 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...

  8. HUST 1698 - 电影院 组合数学 + 分类思想

    http://acm.hust.edu.cn/problem/show/1698 题目就是要把一个数n分成4段,其中中间两段一定要是奇数. 问有多少种情况. 分类, 奇数 + 奇数 + 奇数 + 奇数 ...

  9. .net 环境下c# 通信

    .net环境下通信主要掌握 通信协议(UDP&TCP). 网络抓包工具().:使用方法 点对点通信,IP组播,广播通信 c#中结构体转为字节流方式 c#结构体与c++结构体转换对应关系 开源的 ...

  10. 如需在APP中使用腾讯QQ登陆,需提前申请获取腾讯QQ的APPKEY和APPSecret。

    如需在APP中使用腾讯QQ登陆,需提前申请获取腾讯QQ的APPKEY和APPSecret. 申请流程如下: 步骤1:登陆腾讯开放平台.链接地址: http://open.qq.com/ . 步骤2:填 ...