POJ滑雪
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 97172 | Accepted: 36843 |
Description
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
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 的无后效性,从一个点出发以后是不可能回到这个点的。我们需要按照高度从到到小来转移,这样写起来未免会有些麻烦,代码上不好实现。借助于记忆化搜索的方法可以解决这个问题,记忆化搜索的思想是对于每个状态,只要搜索一次以后,记录下这个状态的最优值,以后在需要用到这个状态就不必要搜索了,因为无后效性,最优永远都不变。
#include <iostream>
#include <cstring>
using namespace std;
int a[][];
int dp[][];
int r, c;
int xx[]={,-,,};
int yy[]={,,,-};
int dfs(int x,int y){
if (dp[x][y]!=-){
return dp[x][y];
}
int ret=;
for (int i=;i<;++i){
int tx=x+xx[i];
int ty=y+yy[i];
if (tx>= && tx<r && ty>= && ty<c && a[tx][ty]<a[x][y]){
ret=max(ret,dfs(tx,ty));
}
}
return dp[x][y]=ret+;
} int main(){
scanf("%d%d",&r,&c);
for (int i=;i<r;++i){
for (int j=;j<c;++j){
scanf("%d",&a[i][j]);
}
}
memset(dp,-,sizeof(dp));
int ans=;
for (int i=;i<r;++i){
for (int j=;j<c;++j){
if (dp[i][j] == -){
ans = max(ans,dfs(i,j));
}
}
}
printf("%d\n", ans);
system("pause");
return ;
}
代码
POJ滑雪的更多相关文章
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- 91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- poj3月题解
poj2110 二分答案+bfs判定 poj2112 二分答案+最大流判定(二分答案真乃USACO亲儿子) poj1986 裸的LCA,值得注意的是,树中任意两点的距离可以等于这两点到根的距离减去2* ...
- POJ 1088 滑雪(记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 92384 Accepted: 34948 Description ...
- POJ 1088 滑雪 -- 动态规划
题目地址:http://poj.org/problem?id=1088 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- OpenJudge/Poj 1088 滑雪
1.链接地址: bailian.openjudge.cn/practice/1088 http://poj.org/problem?id=1088 2.题目: 总Time Limit: 1000ms ...
- poj 1088 滑雪(区间dp+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...
- POJ 1088 滑雪(记忆化搜索+dp)
POJ 1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 107319 Accepted: 40893 De ...
- POJ 1088 滑雪 【记忆化搜索经典】
题目链接:http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
随机推荐
- Cadence OrCAD Cpature创建Title Block
为了统一部门设计文档的管理,需要对文档命名.设计文件符号.设计文件规范做出规定.这里介绍下Cadence OrCAD Cpature原理图设计中创建满足自己的Title Block. 1.创建库文件 ...
- Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题
(转载)Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题 这几天在用spring boot开发项目, 在开发的过程中遇到一个问题hibernate在执 ...
- 图片编辑工具GIMP
今天修改图片: 给图片添加alpha通道,选中要删去的部分,就会变成透明,要保存为png格式 文库参考: http://wenku.baidu.com/link?url=HR1lKoBKS1xbhUJ ...
- KMP Algorithm 字符串匹配算法KMP小结
这篇小结主要是参考这篇帖子从头到尾彻底理解KMP,不得不佩服原作者,写的真是太详尽了,让博主产生了一种读学术论文的错觉.后来发现原作者是写书的,不由得更加敬佩了.博主不才,尝试着简化一些原帖子的内容, ...
- HDFS基础配置
HADOOP-3.1.0-----HDFS基础配置 执行步骤:(1)配置集群(2)启动.测试集群增.删.查(3)执行wordcount案例 一.配置集群 1.在 hadoop-env.sh配置文件添加 ...
- HTTP 错误 405.0 - Method Not Allowed 无法显示您正在查找的页面,因为使用了无效方法(HTTP 谓词)。
x 前言:报错信息 HTTP 错误 405.0 - Method Not Allowed 无法显示您正在查找的页面,因为使用了无效方法(HTTP 谓词). 发送至 Web 服务器的请求使用了为处理该请 ...
- appium api笔记
打印上下文driver.contexts打印当前上下文driver.contextdriver.current_context切换上下文driver.switch_to.context('WEBVIE ...
- react-native-Cocoapods-Swift-Project
https://reactnative.cn/docs/integration-with-existing-apps/ 1.创建一个xcode工程,single View就行,项目语言选择swift, ...
- poj1164
#include<iostream> using namespace std; ][]; ][]; int roomnum; int maxroom; int m,n; typedef s ...
- RoR - Nested Resources, Security ,pagination
root to: 'xxx' 默认root路径 Nested Resource: Rails.application.routes.draw do resources :books do resou ...