问题描述
小袁非常喜欢滑雪, 因为滑雪很刺激。为了获得速度,滑的区域必须向下倾斜,
而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。
小袁想知道在某个区域中最长的一个滑坡
。区域由一个二维数组给出。数组的每个数字代表点的高度。如下:
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。
在上面的例子中,一条可滑行的滑坡为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
import java.util.Scanner;
public class 最长滑雪道 {
static int m,n;
static int[][] map;
static int[][] maker;
static int cnt = 0;
static int[][] dir = {{-1,0},{1,0},{0,1},{0,-1}};
static int h3=0;
static int h4=0;
static int res=-1;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
map = new int[m+1][n+1];
maker = new int[m+1][n+1];
for(int i=0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
int temp = sc.nextInt();
map[i][j] = temp;
}
}
//把每一个结点都深搜一次
for(int i = 0;i<m*n;i++)
{
dfs1(i/m,i-n*(i/m),1);
}
System.out.println(res);
sc.close();
}
private static void dfs1(int x, int y,int sum) {
if(sum>res)
{
res = sum;
}
for(int i = 0;i<4;i++)
{
int tx = x+dir[i][0];
int ty = y+dir[i][1];
if(in(tx, ty))
continue;
if(maker[tx][ty]==0&&map[tx][ty]<map[x][y])
{
maker[tx][ty] = 1;
dfs1(tx, ty, sum+1);
maker[tx][ty] = 0;
}
}
return;
}
private static boolean in(int tx, int ty) {
if(tx<0||ty<0||tx>=m||ty>=n)
return true;
return false;
}
}