POJ1088滑雪
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滑雪的更多相关文章
- POJ1088 滑雪
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- POJ1088滑雪(dp+记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 86411 Accepted: 32318 Description ...
- POJ1088滑雪(记忆化搜索+DFS||经典的动态规划)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 84297 Accepted: 31558 Description M ...
- [POJ1088] 滑雪(递归dp)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- POJ-1088 滑雪 (记忆化搜索,dp)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 86318 Accepted: 32289 Description Mich ...
- poj1088 滑雪 解题报告
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 77423 Accepted: 28779 Description ...
- ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- poj1088滑雪最短路径
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 97281 Accepted: 36886 Description ...
- 经典DP问题--poj1088滑雪
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
随机推荐
- IT人为什么难以拿到高薪?
最近在论坛里看到很多人发牢骚,说薪水少,可在我看来,你们这样的人拿得到高薪才怪! 我先问一句:这里有多少人是本科的?有多少人是正规本科的(不算自考,成考和专升本)?有多少人是有学位的?有多少有学位的是 ...
- sql数据库的表连接方式图文详解
sql数据库表连接,主要分为:内连接.外连接(左连接.右连接 .全连接).交叉连接,今天统一整合一下,看看他们的区别. 首先建表填充值. 学生表:student(id,姓名,年龄,性别 ) 成绩表 ...
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...
- 单点登陆CAS安装过程中可能遇到的问题
可能遇到的问题: 错误: java.security.cert.CertificateException: No name matching localhost found 原因: keystore里 ...
- Linux 下Valgrind 使用
Valgrind包括如下一些工具: Memcheck.这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内 ...
- 设计模式之职责链模式(Chain of Responsibility)
职责链模式原理: 职责链模式和装饰模式以及组合模式类似的地方是都维持着指向父类的指针, 不同点是职责链模式每个子类都继承父类的指针及每个之类都维持着指向父类的指针,而组合模式与装饰模式是组合类鱼装饰类 ...
- 【概率DP/高斯消元】BZOJ 2337:[HNOI2011]XOR和路径
2337: [HNOI2011]XOR和路径 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 682 Solved: 384[Submit][Stat ...
- org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Server returned error code = 404 for URI.. Check server logs for details
严重: Servlet.service() for servlet jsp threw exceptionorg.codehaus.xfire.XFireRuntimeException: Could ...
- 6-Highcharts曲线图之带标识
<!DOCTYPE> <html lang='en'> <head> <title>6-Highcharts曲线图之带标识</title> ...
- fiddler 新发现
就一句话,记录一下 urlreplace baidu.com taobao.com //Fiddler2\Scripts\SampleRules.js 这里发现的 case "urlrepl ...