poj 1088(记忆化搜索)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 88560 | Accepted: 33212 |
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
Source
以前真是太水了。。竟然是看了解题报告觉得好神奇 前几个月的咸鱼,现在叫菜鸟吧2333
重写一遍,一个记忆化搜索的基础题。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = ;
int mp[N][N];
int dp[N][N]; ///dp[i][j]保存的是当前点能划最远的距离
int n,m;
int dir[][] ={{,},{-,},{,},{,-}};
int dfs(int x,int y){
int ans = ;
if(dp[x][y]>) return dp[x][y]; ///记忆化搜索:已经搜过剪枝
for(int i=;i<;i++){
int nextx = x+dir[i][];
int nexty = y+dir[i][];
if(nextx<||nextx>n||nexty<||nexty>m) continue;
if(mp[nextx][nexty]<mp[x][y]){
ans = max(ans,dfs(nextx,nexty));
}
}
dp[x][y]=ans+; ///子问题的解加上1就是当前点的解
return dp[x][y];
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
scanf("%d",&mp[i][j]);
dp[i][j] = ; ///初始化每个点的高度为自身
}
int ans = -;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
dfs(i,j);
if(dp[i][j]>ans) ans = dp[i][j];
}
}
printf("%d\n",ans);
} }
poj 1088(记忆化搜索)的更多相关文章
- 滑雪(POJ 1088 记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 88094 Accepted: 33034 Description ...
- POJ 1191 记忆化搜索
(我是不会告诉你我是抄的http://www.cnblogs.com/scau20110726/archive/2013/02/27/2936050.html这个人的) 一开始没有想到要化一下方差的式 ...
- Test for Job (poj 3249 记忆化搜索)
Language: Default Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9733 A ...
- POJ 2704 Pascal's Travels 【DFS记忆化搜索】
题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS Memory Limit: 65536K Tota ...
- POJ 1579 Function Run Fun 【记忆化搜索入门】
题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Tota ...
- 专题1:记忆化搜索/DAG问题/基础动态规划
A OpenJ_Bailian 1088 滑雪 B OpenJ_Bailian 1579 Function Run Fun C HDU 1078 FatMouse and Chee ...
- POJ 1088 滑雪(记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 92384 Accepted: 34948 Description ...
- POJ 1088 滑雪 DFS 记忆化搜索
http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...
- POJ 1088 滑雪【记忆化搜索】
题意:给出一个二维矩阵,要求从其中的一点出发,并且当前点的值总是比下一点的值大,求最长路径 记忆化搜索,首先将d数组初始化为0,该点能够到达的路径长度保存在d数组中,同时把因为路径是非负的,所以如果已 ...
随机推荐
- Java的各种中文乱码解决方法
一.Servlet输出乱码 1. 用servlet.getOutStream字节流输出中文,假设要输出的是String str ="钓鱼岛是中国的,无耻才是日本的". 1.1 若是 ...
- 爬虫实例——爬取淘女郎相册(通过selenium、PhantomJS、BeautifulSoup爬取)
环境 操作系统:CentOS 6.7 32-bit Python版本:2.6.6 第三方插件 selenium PhantomJS BeautifulSoup 代码 # -*- coding: utf ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- HDU 4313树形DP
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU1507二分图
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- xml 通过正则抓取字段
$str = '<xml> <appid><![CDATA[wxd49ea66070209a6e]]></appid> <bank_type> ...
- Spring Boot 启动报错 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 37
使用命令 java -jar springBoot.jar 启动项目,结果报错如下: Exception at java.lang.String.substring(String.java:) at ...
- Object.defineProperty 与 属性描述符
为JavaScript对象新增或者修改属性,有两种不同方式:直接使用=赋值或者使用Object.defineProperty 定义,使用后者的话还可以设置属性的描述符. Object.definePr ...
- mysql 添加字段 修改字段为not null
添加一个字段 ALTER TABLE jw_user_role ADD zk_env VARCHAR(16); 修改字段为not null,还要把原来的类型也写出来 ALTER TABLE jw_us ...
- python基础--结构篇
在C/C++/Java中,main是程序执行的起点,Python中,也有类似的运行机制,但方式却截然不同: Python使用缩进对齐组织代码的执行,所有没有缩进的代码(非函数定义和类定义),都会在载入 ...