[POJ1088] 滑雪(递归dp)
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
import java.util.Scanner;
public class Main {
public static int[][] answer;
public static void main( String[] args ) {
Scanner sc = new Scanner( System.in );
while( sc.hasNext() ) {
int m = sc.nextInt();
int n = sc.nextInt();
//数据
int matrix[][] = new int[ m ][ n ];
//记录表
answer = new int[m+1][n+1];
for( int i = 0; i < m; i++ ) {
for( int j = 0; j < n; j++ ) {
matrix[ i ][ j ] = sc.nextInt();
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
Main.slove( matrix, i, j );
}
}
int ans = 0;
//取出长度最大的结果
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(answer[i][j] > ans){
ans = answer[i][j];
}
}
}
System.out.println(ans);
answer = null;
matrix = null;
}
}
private static int slove( int[][] matrix, int i, int j ) {
int max = 0;
//退出条件2
if(answer[i][j] > 0 ){
return answer[i][j];
}
if(constraint( i-1, j,matrix.length, matrix[i].length ) && matrix[i][j] > matrix[i-1][j]){
int temp = slove( matrix, i-1, j );
max = temp>max?temp:max;
}
if(constraint( i+1, j,matrix.length, matrix[i].length ) && matrix[i][j] > matrix[i+1][j]){
int temp = slove( matrix, i+1, j );
max = temp > max?temp:max;
}
if(constraint( i, j+1,matrix.length, matrix[i].length ) && matrix[i][j] > matrix[i][j+1]){
int temp = slove( matrix, i, j+1 );
max = temp > max?temp:max;
}
if(constraint( i, j-1,matrix.length, matrix[i].length ) && matrix[i][j] > matrix[i][j-1]){
int temp = slove( matrix, i, j-1 );
max = temp > max?temp:max;
}
answer[i][j] = max + 1;
//退出条件1
return answer[i][j];
}
//边界约束
private static boolean constraint( int x, int y, int m, int n ) {
if( x >= 0 && x < m && y >= 0 && y < n ) {
return true;
}
return false;
}
}
[POJ1088] 滑雪(递归dp)的更多相关文章
- POJ1088滑雪(dp+记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 86411 Accepted: 32318 Description ...
- ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- POJ-1088 滑雪 (记忆化搜索,dp)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 86318 Accepted: 32289 Description Mich ...
- 经典DP问题--poj1088滑雪
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- POJ1088:滑雪(简单dp)
题目链接: http://poj.org/problem?id=1088 题目要求: 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.求可以滑落的最长长度. 题目解析: 首先要先排一 ...
- poj1088 滑雪 dp+dfs记忆化
简单的搜索,不必多说了,初始状态下每个点能到达的长度是1,它本身.还有,注意关掉文件重定向,被坑好多次了. 代码如下: #include<cstdio> #include<algor ...
- POJ1088 滑雪
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- POJ1088滑雪(记忆化搜索+DFS||经典的动态规划)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 84297 Accepted: 31558 Description M ...
随机推荐
- awakeFromNib与initWithCoder
- js模块化开发——require.js学习总结
1.为什么使用require.js 作为命名空间: 作为命名空间使用: 异步加载js,避免阻塞,提高性能: js通过require加载,不必写很多script 2.require.js的加载 requ ...
- MySQL删除单列重复或多列同时重复值并保留一条
在生产环境中,我们有的列是不允许出现重复值的,亦或是某两列不允许同时重复,但由于前端未做限制,或者没限制住,出现了单列重复值,或者两列本应组成唯一组合却也出现重复,这两种情况都是不允许的.现在由于前端 ...
- treegrid.bootstrap使用说明
treegrid.bootstrap使用说明 这个插件是用来做有层次的表格的,大概如图: 官网 http://maxazan.github.io/jquery-treegrid/ 使用这个控件之前需 ...
- 报错找不到jquery-1.10.2.min.map解决办法
http://fruithardcandy.iteye.com/blog/1941452
- H.264 SVC 与H.264 AVC
分级视频编码技术实现一次性编码产生具有不同帧率.分辨率的视频压缩码流,然后根据不同网络带宽.不同的显示屏幕和终端解码能力选择需要传输的视频信息量,以此实现视频质量的自适应调整 AVC 实际上是 H.2 ...
- mesos 资源分配
Mesos 资源分配 众所周知, Mesos在运行时使用wDRF( Dominant Resource Fairness)算法进行一级资源分配, 通过应用程序(Framework)运行时使用资源进行二 ...
- 如何使用矩阵乘法加速动态规划——以[SDOI2009]HH去散步为例
对这个题目的最初理解 开始看到这个题,觉得很水,直接写了一个最简单地动态规划,就是定义 f[i][j]为到了i节点路径长度为j的路径总数, 转移的话使用Floyd算法的思想去转移,借助这个题目也理解了 ...
- C# 6 与 .NET Core 1.0 高级编程 - C# 6 改进
个人原创译文,转载请注明出处.有不对的地方欢迎指出与交流. 英文原文:Professional C# 6 and .NET Core 1.0 - What's New in C# 6 C# 6 改进 ...
- ArcGIS制图表达Representation实战篇1-边界线和行道树制作
ArcGIS制图表达Representation实战篇1-边界线和行道树制作 by 李远祥 即便是有了一些制图表达的基础,很多人还是对ArcGIS制图表达理解停留在表面,因为没有实际的强化训练是很难体 ...