问题描述
小袁非常喜欢滑雪, 因为滑雪很刺激。为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。 小袁想知道在某个区域中最长的一个滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。如下:
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-…-3-2-1更长。事实上,这是最长的一条。
你的任务就是找到最长的一条滑坡,并且将滑坡的长度输出。 滑坡的长度定义为经过点的个数,例如滑坡24-17-16-1的长度是4。
输入格式
输入的第一行表示区域的行数R和列数C(1<=R, C<=10)。下面是R行,每行有C个整数,依次是每个点的高度h(0<= h <=10000)。
输出格式
只有一行,为一个整数,即最长区域的长度。
样例输入
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
样例输出
25
思路:对于区域内每个点进行dfs,对每个点来说进行四个方向的dfs取最大值,然后取所有点为起点的最大长度的最大值,即为答案。
代码如下:
import java.util.*;
public class Main{
static int[][] a, dp;
static int[][] next = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
static int r, c;
public static int dfs(int x, int y) {
if(dp[x][y] > 0) return dp[x][y];//记忆化搜素:如果这个点所能走的最大长度已经算过了,就直接返回
int k;
int maxheight = 1;//最大长度初始为1
for(k=0; k<4; k++) {
int tx = x + next[k][0];
int ty = y + next[k][1];
if(tx < 0 || tx >= r || ty < 0 || ty >= c) continue;
if(a[tx][ty] >= a[x][y]) continue;
maxheight = Math.max(maxheight, dfs(tx, ty) + 1);//取四个方向的最大值
}
return dp[x][y] = maxheight;//存储该值并返回
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, j;
r = sc.nextInt();
c = sc.nextInt();
a = new int[r][c];
dp = new int[r][c];
for(i=0; i<r; i++)
for(j=0; j<c; j++) {
dp[i][j] = 0;
a[i][j] = sc.nextInt();
}
int ans = 0;
for(i=0; i<r; i++)
for(j=0; j<c; j++) {
ans = Math.max(ans, dfs(i, j));
}
System.out.println(ans);
}
}