[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 ...
随机推荐
- CentOS 6.6下JDK1.7安装与配置(Linux)经典入门详解案例
最近用的linux较多,在网站找了一些关于linux环境下jdk安装的教程,过程是有的但是好多细节都没有表现出来,所以我花了点时间总结了一下,希望对大家都有帮助... CentOS下JDK1.7安装与 ...
- Linux下配置Apache最大连接数
最近有博友发现我的博客经常http 503,博客负载不大,应该不会出现负载问题,很有可能就是Apache最大连接数原因,Apache默认支持150个连接.1.先要修改最大连接数,必须了解Apache的 ...
- easyui弹出窗关闭前调用确认窗口,先关闭页面后调用弹出窗口
弹出窗关闭的时候提示是否关闭,同时进行一些对应的方法调用, 然而在进行页面关闭调用的时候,往往页面关闭了,才弹出确认对话框, $.messager.confirm和panel的onBeforeClos ...
- Objective-C Effective 技巧
1.除非有必要,否则不要引用头文件,一般来说应该利用@class使用前向声明,并在实现中引用头文件:如果实在无法使用,比如要声明某个类遵循一项协议,这种情况下,尽量把这条声明移到分类中,如果不行的话, ...
- pureMVC介绍及学习
1 简介 Pure MVC是在基于模型.视图和控制器MVC模式建立的一个轻量级的应用框架,这种开源框架是免费的,它最初是执行的ActionScript 3语言使用的Adobe Flex.Flash ...
- Canvas createImageData
createImageData() 方法创建新的空白 ImageData 对象.新对象的默认像素值 transparent black. 对于 ImageData 对象中的每个像素,都存在着四方面的信 ...
- ASP.NET Core MVC压缩样式、脚本及总是复制文件到输出目录
前言 在.NET Core之前对于压缩样式文件和脚本我们可能需要借助第三方工具来进行压缩,但在ASP.NET MVC Core中则无需借助第三方工具来完成,本节我们来看看ASP.NET Core MV ...
- cygwin 运行窗口程序
首先, 默认安装的cygwin是不能运行窗口程序的 比如,一段python窗口程序: import * from tkinter Tk() mainloop() 如果使用命令行: python3 py ...
- Codeforces 712B
B. Memory and Trident time limit per test:2 seconds memory limit per test:256 megabytes input:standa ...
- C++ 构造函数和析构函数的调用顺序、虚析构函数的作用
构造函数和析构函数的调用顺序 构造函数的调用顺序: 当建立一个对象时,首先调用基类的构造函数,然后调用下一个派生类的构造函数,依次类推,直至到达最底层的目标派生类的构造函数为止. 析构函数的调用书序: ...