[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 ...
随机推荐
- Java中两个List对比的算法
Java中两个List对比的算法: // 测试数据 // tdcsDdt.add("Z"); // tdcsDdt.add("B"); // tdcsDdt ...
- Eclipse寻找JVM的机制
Eclipse寻找JVM的机制 查看当前用了哪个jvm的方法: Help->About Eclipse -> Installation Details ->Configuration ...
- 初识JSON
▓▓▓▓▓▓ 大致介绍 JSON(JavaScript Object Notation JavaScript对象表示法),JSON是一种数据格式,不是一种编程语言.虽然它的名字中有JavaScrip ...
- Swift3.0服务端开发(五) 记事本的开发(iOS端+服务端)
前边以及陆陆续续的介绍了使用Swift3.0开发的服务端应用程序的Perfect框架.本篇博客就做一个阶段性的总结,做一个完整的实例,其实这个实例在<Swift3.0服务端开发(一)>这篇 ...
- Java程序打开指定地址网页
1.今天遇到了需要手动输入http地址打开指定网页的需求,试着做一个用程序打开指定网页的功能,搜了一下,还真有一个现成的例子,稍加改造,实现自己的需求: 2.代码不多,两个文件:如下: package ...
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
@property与@synthesize是成对出现的,可以自动生成某个类成员变量的存取方法.在Xcode4.5以及以后的版本,@synthesize可以省略. 1.atomic与nonatomica ...
- [CSS3] 学习笔记-HTML与CSS简单页面效果实例
一个简单的首页的设计: html文件: <!doctype html> <html> <head> <meta charset="UTF-8&quo ...
- Ubuntu系统下搭建PPTP类型VPN环境
step1: 安装pptpd 很简单的命令:sudo apt-get install pptpd step2: 修改pptpd的配置 有三个文件需要修改: (1)修改/etc/pptpd.conf,添 ...
- ADODB——RecordSet对象
转自网友,看着挺全就转了,供大家学习研究. Recordset 对象的属性 1.CursorType 属性 AdOpenForwardOnly: 仅向前游标,默认值.除了只能在记录中向前滚动外,与静态 ...
- Spring BeanFactory实例化Bean的详细过程
Spring中Bean的实例化是Bean生命周期的一个重要环节,通常Bean初始化后将不再改变. 那么Spring实例Bean的过程到底是怎么样的呢?! 要想获取到一个bean对象,得先通过Bea ...