[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 ...
随机推荐
- javascript的字符串操作
一,把字符串的首字母大写返回一个新的字符串 1.1简单写法,把一个单词的首字母大写 String.prototype.firstUpperCase = function(){ return this[ ...
- 在ubuntu下增加root用户并登录
1.打开终端. 2.输入sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 3.在弹出的编辑框里输入:greeter-show-ma ...
- MyBatis 一对一关联查询
xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...
- [nodejs] day1-创建服务器
一.使用匿名函数(新建文件service.js)创建一个服务器: var http = require("http"); //Node.js自带的 http 模块,并且把它赋值给 ...
- [原创.数据可视化系列之十二]使用 nodejs通过async await建立同步数据抓取
做数据分析和可视化工作,最重要的一点就是数据抓取工作,之前使用Java和python都做过简单的数据抓取,感觉用的很不顺手. 后来用nodejs发现非常不错,通过js就可以进行数据抓取工作,类似jqu ...
- 对JavaScript中this的理解
JavaScript中的this其实没传说中的那么难,也没那么乱. 我们来分析下,this主要是跟它的执行环境有关. 而通常情况下,this都是放在函数体中或可执行的JS代码中(函数体除外). 至于J ...
- Vuex 模块化与项目实例 (2.0)
Vuex 强调使用单一状态树,即在一个项目里只有一个 store,这个 store 集中管理了项目中所有的数据以及对数据的操作行为.但是这样带来的问题是 store 可能会非常臃肿庞大不易维护,所以就 ...
- 在 Windows 上安装 Hadoop 教程(转)
在 Windows 上安装 Hadoop 教程 一见 2010.1.6 www.hadoopor.com/hadoopor@foxmail.com 1. 安装 JDK 不建议只安装 JRE,而是建议直 ...
- C# 多态理论基础
一.概述 同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果,这就是多态性. 可以用不同的方式实现组件中的多态性: ● 接口多态性. ● 继承多态性. ● 通过抽象类实现的多态性. 二.实 ...
- SwaggerUI ASP.Net WebAPI2
目前在用ASP.NET的 WebAPI2来做后台接口开发,在与前台做测试的时候,总是需要发送一个demo给他,但是这样很麻烦的,他还有可能记不住. 然后就想到SwaggerUI 生成测试文档. 话不多 ...