Nyoj 10 skiing
时间限制:3000 ms | 内存限制:65535 KB
难度:5
描述
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。
后面是下一组数据;
输出
输出最长区域的长度。
样例输入
1
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 classMain {
int[][] direction = {
{-1,0},
{0,1},
{1,0},
{0,-1}
};
public static void init(map[][] maps){
for(int i = 0;i<maps.length;i++){
mapm = new map();
m.height = 10500;
m.maxLength = 0;
maps[i][0]=m;
}
for(int i = 0;i<maps.length;i++){
mapm = new map();
m.height = 10500;
m.maxLength = 0;
maps[i][maps[0].length-1] = m;
}
for(int i = 0;i<maps[0].length;i++){
mapm = new map();
m.height = 105000;
m.maxLength = 0;
maps[0][i]=m;
}
for(int i = 0;i<maps[0].length;i++){
mapm = new map();
m.height = 10500;
m.maxLength = 0;
maps[maps.length-1][i] = m;
}
}
public intsearch_maxLength(map[][] maps,intx, int y){
int i, maxLength , length;
maps[x][y].maxLength = 1;
maxLength = length = 0;
for(i = 0;i<4;i++){
if(maps[x+direction[i][0]][y+direction[i][1]].height<maps[x][y].height){
if(maps[x+direction[i][0]][y+direction[i][1]].maxLength==0){
length=search_maxLength(maps, x+direction[i][0], y+direction[i][1]);
}else{
length = maps[x+direction[i][0]][y+direction[i][1]].maxLength;
}
if(length>maxLength){
maxLength = length;
}
}
}
return maps[x][y].maxLength+=maxLength;
}
public static void main(String[] args) {
Scannerreader= newScanner(System.in);
int N = reader.nextInt();
while(N-->0){
int rowCount = reader.nextInt();
int colCount = reader.nextInt();
int length = 0;
int maxLength = 0;
map[][]maps= newmap[rowCount+2][colCount+2];
init(maps);
for(int i = 1;i<=rowCount;i++){
for(int j = 1;j<=colCount;j++){
mapm = new map();
m.height = reader.nextInt();
m.maxLength = 0;
maps[i][j] = m;
}
}
/*for(int i = 0;i<maps.length;i++){
for(intj = 0;j<maps[0].length;j++){
mapm = new map();
m= maps[i][j];
System.out.print(m.height+"");
}
System.out.println();
}*/
for(int i = 1;i<=rowCount;i++){
for(int j = 1;j<=colCount;j++){
if(maps[i][j].maxLength==0){
length = newMain().search_maxLength(maps, i, j);
if(length>maxLength)
maxLength = length;
}
}
}
System.out.println(maxLength);
}
reader.close();
}
}
class map{
int height;
int maxLength;
}