题目链接: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. jenkins配置RF构建结果显示

    声明:转载请注明出处,谢谢 步骤1:安装robot framework plugin插件:系统管理-管理插件 步骤2:设置构建后操作:job-配置-构建后操作增加“Publish Robot Fram ...

  2. 【RF库Collections测试】Remove Duplicates

    Name:Remove DuplicatesSource:Collections <test library>Arguments:[ list_ ]Returns a list witho ...

  3. 系统日志:/var/log/messages

    /var/log/messages 存放的是系统的日志信息,它记录了各种事件,基本上什么应用都能往里写日志,在做故障诊断时可以首先查看该文件内容 [root@mirh5_center1_111.231 ...

  4. butterknife用法总结

  5. Handler基本用法

    片断一:mHandler = new Handler();mRunnable = new Runnable() { @Override public void run() { currentPosit ...

  6. 《JavaScript 秘密花园》

    恰巧今天是传统民间重要的节日之一--七夕节: 被大家挂在嘴上最多的一句话便是:有对象了吗?这不-- 这样的话,那咱就先给new出一个对象吧: var boyfriend = new Object(); ...

  7. Use Reentrant Functions for Safer Signal Handling(译:使用可重入函数进行更安全的信号处理)

    Use Reentrant Functions for Safer Signal Handling 使用可重入函数进行更安全的信号处理 How and when to employ reentranc ...

  8. 网络编程之HttpClient类(转)

    12.2 网络编程之HttpClient类 除了可以使用HttpWebRequest类来实现HTTP网络请求之外,我们还可以使用HttpClient类来实现.对于基本的请求操作,HttpClient类 ...

  9. 原生js(三)

    客户端js的时间线: 1.web浏览器创建Document对象,开始解析html和文本.生成Element对象和Text节点添加到文档中.这个阶段的document.readystate==" ...

  10. filter对数组和对象的过滤

    1,对数组的过滤 let arr = ['1', '2', '3'] let b = arr.filter(val => val === '2') console.log(b) // ['2] ...