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数组中,同时把因为路径是非负的,所以如果已 ...
随机推荐
- Eclipse ADT插件 匹配的sdk tools版本
Eclipse android ADT插件最后的版本为ADT 23.0.7 (August 2015),google不再更新. 和之匹配的android tools版本为SDK Tools r24.1 ...
- SSO解决session共享的几种方案
之前做项目遇到了这个sso系统,当时只是理解了一部分,今天偶尔发现一篇文章,觉得写的不错,增加了sso知识: 单点登录在现在的系统架构中广泛存在,他将多个子系统的认证体系打通,实现了一个入口多处使用, ...
- HDU1260DP
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 简单的web小程序
首先我们先编写一个jsp表面的程序, <%@ page language="java" contentType="text/html; charset=UTF-8& ...
- Difference between List View and DataGrid in WPF
Well, in WPF the difference between ListView and DataGrid is just one. Editing. You need editing use ...
- [LeetCode] 22. Generate Parentheses ☆☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 51Nod 1014 X^2 Mod P
注意潜在范围 x*x用long long #include <bits/stdc++.h> using namespace std; typedef long long LL; #defi ...
- Codeforces 321E Ciel and Gondolas
传送门:http://codeforces.com/problemset/problem/321/E [题解] 首先有一个$O(n^2k)$的dp. # include <stdio.h> ...
- 【BZOJ】2502 清理雪道
[算法]有源汇上下界最小流 [题解]上下界 初看以为是最小覆盖,发现边可以重复经过,不对. 要求所有边都经过……那就下界为1,上界为inf的可行流. 源汇……S连入度为0的点,T连出度为0的点?(反正 ...
- 【转载】Lua中实现类的原理
原文地址 http://wuzhiwei.net/lua_make_class/ 不错,将metatable讲的很透彻,我终于懂了. --------------------------------- ...