[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 ...
随机推荐
- C++第四天学习
回顾: 1.初始化表 2.this指针 3.拷贝构造 Test(const Test& rt) { //1.分配新空间 //2.给新空间赋值 } 4.static成员 类::函数(): 类型 ...
- Java Swing Graphics Graphics2D的一般用法
Java Swing Graphics Graphics2D的一般用法: 贝塞尔曲线参考:http://www.zhangxinxu.com/wordpress/2014/06/deep-unders ...
- 去除 MyEclipse updating index
去除 MyEclipse updating index http://zhidao.baidu.com/link?url=OfHjTTxnNRoijnsaweBl3K3UTlnlFGdtHEQIvEW ...
- 为应用程序的选项卡及ActionBar设置样式
示例文件 flex-mobile-dev-tips-tricks-pt2.zip 关于Flex移动开发的提示和技巧有一系列文章,这是其中的第二部分.第一部分集中讲解如何在视图切换及应用程序操作切换之 ...
- Fiddler 模拟请求的操作方法
此文记录使用Fidder Web Debugger工具,模拟请求的操作步骤! 首先简述一下fiddler的使用: 1.下载安装Fidder抓包工具. 2.打开fiddler发现有左边的栏有请求的url ...
- Linux下使用javac编译
Linux下使用javac编译Hadoop程序 首先要配置好Hadoop, 给出两个教程 Hadoop安装教程单机/伪分布式配置Hadoop2.6.0/Ubuntu14.04 Hadoop集群安装配置 ...
- HDU 2568[前进]模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2568 关键思想:傻傻地模拟 代码如下: #include<iostream> using ...
- 分布式搜索之搭建Solrcloud(Solr集群)
Solrcloud介绍: SolrCloud(solr集群)是Solr提供的分布式搜索方案. 当你需要大规模,容错,分布式索引和检索能力时使用SolrCloud. 当索引量很大,搜索请求并发很高时,同 ...
- Spring Boot 基础教程系列学习文档
Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTfull API简单项目的快速搭建 Spring Boot基础教程3-配置文件 ...
- 【吐血整理】svn命令行,Subversion的正确使用姿势~
一.写在前面 前面一直博主一直用svn的桌面版本,但看项目经理一直都用的命令行方式,不为性能,还能直接装逼呀!在这里先感谢赵哥,也把它分享给感兴趣的你们~ 二.直接上干货 1. svn checkou ...