[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 用户管理
3.使用命令管理账户 useradd 选项 用户名//添加新用户 usermod 选项 用户名//修改已经存在的用户 userdel -r 用户名//删除用户表示自家目录一起删除. grou ...
- SVG的text使用
SVG的text使用: 参考:http://www.docin.com/p-7393979.html <%@ page language="java" contentType ...
- PHP的线程安全与非线程(NTS)安全版本的区别
Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?这里做一个简单的介绍. ...
- Bloom Filter的基本原理和变种
学习一个东西首先要知道这个东西是什么,可以做什么,接着再了解这个东西有什么好处和优势,然后再学习他的工作原理.下面我们分别从这三点简单介绍一下bloom filter,以及和他的变种. What:在允 ...
- android:在ViewPager中使用Button
最近在项目用用到ViewPager ,其中页面包含有Button,因为之前也有使用个ViewPager ,所以这个也照搬之前的方式,测试后发现点击button无法执行,这个button是在第一页面的默 ...
- CardboardSDK-iOS 源码简单分析
该项目地址: 地址 克隆地址为 https://github.com/rsanchezsaez/CardboardSDK-iOS.git 目前如果想在iOS设备上实现双目VR的功能,Google 已经 ...
- 简学Python第二章__巧学数据结构文件操作
#cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...
- ajax问题
1. 代码:var i;for(i=0;i<10;i++){ ajaxServise(i);} 在for循环中调用ajax方法 补充页面上的数据,这样写是错误的,他不会每执行一次fo ...
- spring 定时任务 taskScheduler详解
spring 3.0版本后,自带了一个定时任务工具,而且使用简单方便,不用配置文件,可以动态改变执行状态.也可以使用cron表达式设置定时任务. 被执行的类要实现Runnable接口 TaskSche ...
- GoldenGate 传统抽取进程的 ADG 模式
:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...