题目链接:http://poj.org/problem?id=1088

有两种方法

一是按数值大小进行排序,然后按从小到大进行dp即可;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h> using namespace std; #define N 110
#define met(a, b) memset(a, b, sizeof(a)) typedef long long LL; int n, m, dp[N][N], A[N][N];
int dir[][] = { {-, },{, },{, -},{, } }; struct node
{
int x, y, w;
friend bool operator<(node p, node q)
{
return p.w < q.w;
}
}a[N*N]; int main()
{
while(scanf("%d %d", &m, &n)!=EOF)
{
met(a, );
met(A, );
int len = ;
for(int i=; i<=m; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d", &A[i][j]);
a[len].x = i;
a[len].y = j;
a[len++].w = A[i][j];
}
}
sort(a, a+len);
met(dp, );
int ans = ;
for(int i=; i<len; i++)
{
int Max = ;
for(int j=; j<; j++)
{
int p = a[i].x + dir[j][];
int q = a[i].y + dir[j][];
if(A[p][q] < a[i].w )
Max = max(Max, dp[p][q]);
}
dp[a[i].x][a[i].y] = Max + ;
ans = max(ans, Max+);
}
printf("%d\n", ans);
}
return ;
}
/*
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
*/

还一种是不排序,用记忆化搜索:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h> using namespace std; #define N 110
#define met(a, b) memset(a, b, sizeof(a)) typedef long long LL; int n, m, dp[N][N], A[N][N], ans;
int dir[][] = { {-, },{, },{, -},{, } }; int dfs(int x, int y)
{
if(dp[x][y]) return dp[x][y];
int Max = ;
for(int i=; i<; i++)
{
int px = x+dir[i][];
int py = y+dir[i][];
if(px> && py> && px<=m && py<=n && A[px][py]<A[x][y])
Max = max(Max, dfs(px, py));
}
dp[x][y] = Max+;
ans = max(ans, dp[x][y]);
return dp[x][y];
} int main()
{
while(scanf("%d %d", &m, &n)!=EOF)
{
met(A, );
for(int i=; i<=m; i++)
{
for(int j=; j<=n; j++)
scanf("%d", &A[i][j]);
}
met(dp, ); ans = ; for(int i=; i<=m; i++)
{
for(int j=; j<=n; j++)
{
dfs(i, j);
}
}
printf("%d\n", ans);
}
return ;
}
/*
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
*/

滑雪---poj1088(动态规划+记忆化搜索)的更多相关文章

  1. sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

    Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...

  2. ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  3. POJ-1088 Skiing(记忆化搜索)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  4. 洛谷P1434滑雪题解及记忆化搜索的基本步骤

    题目 滑雪是一道dp及记忆化搜索的经典题目. 所谓记忆化搜索便是在搜索的过程中边记录边搜索的一个算法. 当下次搜到这里时,便直接使用. 而且记忆化搜索一定要满足无后效性,为什么呢,因为如果不满足无后效 ...

  5. Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)

    描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提 ...

  6. Poj-P1088题解【动态规划/记忆化搜索】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=1088 题目描述: 区域由一个二维数组给 ...

  7. UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)

    描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  8. poj1088(记忆化搜索入门题)

    题目链接:http://poj.org/problem?id=1088 思路: 明显的记忆化搜索题,用dp[i][j]表示从(i,j)出发能滑的最远距离,用dfs搜索,若dp[x][y]>0即已 ...

  9. [NOIP2017] 逛公园 (最短路,动态规划&记忆化搜索)

    题目链接 Solution 我只会60分暴力... 正解是 DP. 状态定义: \(f[i][j]\) 代表 \(1\) 到 \(i\) 比最短路长 \(j\) 的方案数. 那么很显然最后答案也就是 ...

随机推荐

  1. linux-nohup后台运行

    先说一下linux重定向: 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出. 在一般使用时,默认的是标准输出,既1.当我们需要特殊用途时,可以使用其他标 ...

  2. Linux应急响应入侵排查思路

    0x00 前言 ​ 当企业发生黑客入侵.系统崩溃或其它影响业务正常运行的安全事件时,急需第一时间进行处理,使企业的网络信息系统在最短时间内恢复正常工作,进一步查找入侵来源,还原入侵事故过程,同时给出解 ...

  3. PHP代码审计笔记--代码执行漏洞

    漏洞形成原因:客户端提交的参数,未经任何过滤,传入可以执行代码的函数,造成代码执行漏洞. 常见代码注射函数: 如:eval.preg_replace+/e.assert.call_user_func. ...

  4. iOS开发-- 设置UIButton的文字显示位置、字体的大小、字体的颜色

    btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...

  5. java.lang.IllegalArgumentException: No converter found for return value of type

    原文地址: http://blog.csdn.net/linhaiguo/article/details/51554766 问题原因: 请求返回的数据无法转换,需要添加如下配置 解决方法: 1.在po ...

  6. ubuntu 手动安装mysql

    申请了一台云主机,需要手动安装所有环境,今天将mysql安装过程记下. 安装mysqla. 下载不了gcc, 需要先运行apt-get updateb. cmake报错,每次要先删除cmakeCach ...

  7. SaltStack salt-ssh 用法

    以下在 master 操作: (1) 我们在安装部署 SaltStack 的时候,需要安装 salt 客户端,还要与 salt 服务端进行认证才能互相通信(2) 如果我们使用 salt-ssh 的方式 ...

  8. php_admin_value open_basedir 引起的上传文件失败解决方法

    为了安全,我们通常会在虚拟主机设置中,加入这一行php_admin_value open_basedir "/usr/local/apache/htdocs/www"但这会导致mo ...

  9. 微信小程序学习指南

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. <转>github入门到上传本地项目

    转自 http://www.cnblogs.com/specter45/p/github.html GitHub是基于git实现的代码托管.git是目前最好用的版本控制系统了,非常受欢迎,比之svn更 ...