Description

Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
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 输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
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 SHTSC 2002

链接: http://poj.org/problem?id=1088

这是POJ经典的记忆化搜索结构题目,很早以前就知道这道题目,今天偶然想起来又看了一眼Discuss,决定将大牛的方法记录下来。 这道题目的意思是,给定一个二维矩阵里,找到一条最长的下降坡道(4-connected,从最大值到最小值为递减的一条path)。

我们主要可以维护一个count[][]矩阵,对上下左右分别用dfs来记录下每个位置的最大count。之后也要做一些pruning,比如当count[i][j] > 0的时候,表示这个位置已经被计算过,可以直接返回count[i][j]来省略重复计算。

import java.util.*;

public class Solution {
private int[][] count; public int findDownHillSki(int[][] board) {
count = new int[board.length][board[0].length]; // matrix count for counting
int max = 0;
for (int i = 0; i < board.length; i++) { // fill count[][] using findMax(i, j, board)
for (int j = 0; j < board[0].length; j++) {
findMax(i, j, board);
max = Math.max(max, count[i][j]);
}
}
return max;
} private int findMax(int i, int j, int[][] board) {
int max = 0;
if (count[i][j] > 0) {
return count[i][j];
} if (i - 1 >= 0) {
if (board[i][j] > board[i - 1][j]) {
max = Math.max(max, findMax(i - 1, j, board));
}
}
if (j - 1 >= 0) {
if (board[i][j] > board[i][j - 1]) {
max = Math.max(max, findMax(i, j - 1, board));
}
}
if (i + 1 < count.length) {
if (board[i][j] > board[i + 1][j]) {
max = Math.max(max, findMax(i + 1, j, board));
}
}
if (j + 1 < count[0].length) {
if (board[i][j] > board[i][j + 1]) {
max = Math.max(max, findMax(i, j + 1, board));
}
}
return count[i][j] = max + 1;
}
}

测试:

public class Program {

    public static void main(String[] args) {            

        int[][] board = {{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}}; int[][] board1 = {{1, 2, 3, 4, 5},
{6, 5, 4, 3, 2}}; Solution sol = new Solution();
int res = sol.findDownHillSki(board);
System.out.println(res);
}
}

Reference:

http://poj.org/showmessage?message_id=113914

POJ1088滑雪的更多相关文章

  1. POJ1088 滑雪

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  2. POJ1088滑雪(dp+记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 86411   Accepted: 32318 Description ...

  3. POJ1088滑雪(记忆化搜索+DFS||经典的动态规划)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 84297   Accepted: 31558 Description M ...

  4. [POJ1088] 滑雪(递归dp)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. POJ-1088 滑雪 (记忆化搜索,dp)

    滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 86318 Accepted: 32289 Description Mich ...

  6. poj1088 滑雪 解题报告

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 77423   Accepted: 28779 Description ...

  7. ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  8. poj1088滑雪最短路径

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 97281   Accepted: 36886 Description ...

  9. 经典DP问题--poj1088滑雪

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

随机推荐

  1. Asp.Net MVC3(三)-MvcApp实现全局异常捕获

    定义异常捕获类: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMu ...

  2. Linux删除文件夹命令

    linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就 ...

  3. PHP错误The server encountered an internal error or misconfiguration and was unable to complete your re

    我的笔记本电脑上的环境安装了很多次,但是运行项目时总是会报The server encountered an internal error or misconfiguration and was un ...

  4. 关于BaseAdapter的使用及优化心得(一)

    对于Android程序员来说,BaseAdapter肯定不会陌生,灵活而优雅是BaseAdapter最大的特点.开发者可以通过构造BaseAdapter并搭载到ListView或者GridView这类 ...

  5. char a[] = "hello"; char c[] = {'h','e','l','l','o'}; int b[] = {1, 2, 3, 4, 5};的长度区别,及内存中空间开辟情况

    1, char a[] = "hello"; char c[] = {'h','e','l','l','o'}; int b[] = {1, 2, 3, 4, 5}; 数组是开辟一 ...

  6. Testing Multi-Tenancy on a Local Machine

    If you are running locally and do not have a domain to map, you can edit your\Windows\System32\drive ...

  7. HDU 2196 求树上所有点能到达的最远距离

    其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...

  8. 【BZOJ】【2756】【SCOI2012】奇怪的游戏

    网络流-最大流 这题……建模部分先略过 这道题是会卡时限的T_T俺的Dinic被卡了,在此放几篇很棒的讲网络流算法的文章,至于大家耳熟能详的论文就不放了…… http://www.cppblog.co ...

  9. Matlab实现单变量线性回归

    一.理论 二.数据集 6.1101,17.592 5.5277,9.1302 8.5186,13.662 7.0032,11.854 5.8598,6.8233 8.3829,11.886 7.476 ...

  10. 使用NPOI和线程池快速加载EXCEL数据

    private void FilterData() { List<Task> tasks = new List<Task>(); IWorkbook workbook = Cs ...