滑雪---poj1088(动态规划+记忆化搜索)
题目链接: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(动态规划+记忆化搜索)的更多相关文章
- 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 ...
- ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- POJ-1088 Skiing(记忆化搜索)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- 洛谷P1434滑雪题解及记忆化搜索的基本步骤
题目 滑雪是一道dp及记忆化搜索的经典题目. 所谓记忆化搜索便是在搜索的过程中边记录边搜索的一个算法. 当下次搜到这里时,便直接使用. 而且记忆化搜索一定要满足无后效性,为什么呢,因为如果不满足无后效 ...
- Codevs_1017_乘积最大_(划分型动态规划/记忆化搜索)
描述 http://codevs.cn/problem/1017/ 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大. 1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提 ...
- Poj-P1088题解【动态规划/记忆化搜索】
本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=1088 题目描述: 区域由一个二维数组给 ...
- UVA_437_The_Tower_of_the_Babylon_(DAG上动态规划/记忆化搜索)
描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- poj1088(记忆化搜索入门题)
题目链接:http://poj.org/problem?id=1088 思路: 明显的记忆化搜索题,用dp[i][j]表示从(i,j)出发能滑的最远距离,用dfs搜索,若dp[x][y]>0即已 ...
- [NOIP2017] 逛公园 (最短路,动态规划&记忆化搜索)
题目链接 Solution 我只会60分暴力... 正解是 DP. 状态定义: \(f[i][j]\) 代表 \(1\) 到 \(i\) 比最短路长 \(j\) 的方案数. 那么很显然最后答案也就是 ...
随机推荐
- requests 安装
requests 是用来发送 HTTP 请求的一个库,requests 是对 urllib 和 urllib2 进行封装的一个模块,用来取代 urllib 和 urllib2,可以使用以下两种方法安装 ...
- 在 Ubuntu 中安装 MySQL 指南
安装MySQL 在Ubuntu上可以使用Ubuntu Software Center或者apt命令来安装MySQL,两种方式都十分方便. 1. 使用Ubuntu Software Center:打开U ...
- Spring系列之IOC容器
一.概述 IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象之间的依赖.应用程序无需直接在代码中new 相关的对象,应用程序由IOC容器进行组装.在S ...
- 【cs229-Lecture3】为什么要选择“最小二乘法”这个指标
视频地址:http://v.163.com/movie/2008/1/E/B/M6SGF6VB4_M6SGHM4EB.html 具体的推导过程,讲义上都有,已经很详细了.这里的推导过程大都是自己为了练 ...
- kafka---->kafka的使用(一)
今天我们来学习一下kafka的简单的使用与配置.世上有可以挽回的和不可挽回的事,而时间经过就是一种不可挽回的事. kafka的安装配置 一.kafka的使用场景 活动跟踪:网站用户与前端应用程序发生交 ...
- 【Spring系列】Spring IoC
前言 IoC其实有两种方式,一种是DI,而另一种是DL,即Dependency Lookup(依赖查找),前者是当前软件实体被动接受其依赖的其他组件被IOc容器注入,而后者是当前软件实体主动去某个服务 ...
- jQuery Sizzle选择器(三)
在Sizzle的入口方法Sizzle()中看到的一个根据浏览器来初始化document各个方法的函数setDocument(),接下来主要看一下这个方法都做了什么. 但之前有必要看一下它用到的一些Si ...
- php guzzle post async
use GuzzleHttp\Pool;use GuzzleHttp\Client;//use GuzzleHttp\Psr7\Request;use Psr\Http\Message\Respons ...
- webstorm启动报错
环境: 在重装完系统的电脑上第一次安装webstorm, 问题: 解决”failed to load jvm dll“的报错问题: 解决方案: 安装Microsoft Visual C++ 2010 ...
- 状态机FSM
参考: 百度-有限状态机 博客园-有限状态机FSM详解及其实现 CSDN-状态机FSM代码框架 腾讯开源项目behaviac 占坑,待编辑...