1088:滑雪
-
总时间限制:
- 1000ms 内存限制:
- 65536kB
描述
-
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更长。事实上,这是最长的一条。
输入
- 输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是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
来源
-
Don't know
第一次写“我为人人型”递推,通过自己已有的条件,来给符合条件的相邻数组更新当前数值。例如:当前在坐标为x,y的地方,如果可以从上方滑雪到x,y的位置(当前高度要低于上方的高度),并且当前到我的所需要的最大步数+1要比当前已有的上方的步数要多,则更新上方高度的最大步数,即h[x-1][y] = h[x][y]+1;这是上方的判断过程,然后顺序判断其它方位是否可行,注意需要对当前的高度由小到大排序来保证访问到的结点没有被其它结点访问过(因为高度小的结点无法访问高度大的结点)。
这里我用java写的代码,需要说明一下使用的方法,由于要对自定义结点进行排序,所以需要内部类实现comparable接口重写compareTo()方法使Arrays的sort方法支持该自定义类的排序,还有,要注意sort方法只能对一维数组排序!还需说明一下Arrays的sort方法会自动调用compareTo()方法进行排序。
static void | sort(Object[] a)
Sorts the specified array of objects into ascending order, according to the
natural ordering of its elements.
|
int | compareTo(T o)
Compares this object with the specified object for order.
|
static void | fill(int[] a, int val)
Assigns the specified int value to each element of the specified array of ints.
|
import java.util.*;
class mapp implements Comparable<mapp> { //内部类
int x, y;
int height;
public mapp(int x, int y, int height) {
this.x = x;
this.y = y;
this.height = height;
}
@Override
public int compareTo(mapp o) {
// TODO Auto-generated method stub
if (this.height > o.height) {
return 1;
} else if (this.height < o.height) {
return -1;
} else {
return 0;
}
}
}
public class Skiing {
static int R, C;
private static Scanner input;
static mapp[] m;
static int[][] step;
static int[][] h;
public static void main(String[] args) {
input = new Scanner(System.in);
R = input.nextInt();
C = input.nextInt();
m = new mapp[R*C];
step = new int[R][C];
h = new int[R][C];
int index = 0 ;
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) {
h[i][j] = input.nextInt();
m[index] = new mapp(i, j, h[i][j]);
index++;
}
Arrays.sort(m);
for( int i = 0 ; i < R ; i++)
Arrays.fill(step[i], 1);
for( int i = 0 ; i < index ; i++){ //我为人人型递推
int x = m[i].x;
int y = m[i].y;
//up
if( x >= 1 && h[x][y] < h[x-1][y] && step[x][y] + 1 > step[x-1][y])
step[x-1][y] = step[x][y] + 1 ;
//right
if( y < C-1 && h[x][y] < h[x][y+1] && step[x][y] + 1 > step[x][y+1])
step[x][y+1] = step[x][y] + 1 ;
//down
if( x < R-1 && h[x][y] < h[x+1][y] && step[x][y] + 1 > step[x+1][y])
step[x+1][y] = step[x][y] + 1 ;
//left
if( y >= 1 && h[x][y] < h[x][y-1] && step[x][y] + 1 > step[x][y-1])
step[x][y-1] = step[x][y] + 1 ;;
}
int max = 0;
for( int i = 0 ; i < R ; i++)
for( int j = 0 ; j < C ; j++)
max = Math.max(max, step[i][j]);
System.out.println(max);
/*index = 0 ;
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++) {
System.out.println(m[index].height);
index++;
}*/
}
}
通过改题目初步认识了“我为人人”型递推的写法,还学习了java自定义类排序的实现。
-
话说java好慢啊,4000+ms,怎么通过的。
-
题目:http://bailian.openjudge.cn/practice/1088