Skiing time limit: 3000 MS | memory limit: 65535 kb difficulty: 5
-
Description
-
It's not surprising that Michael loves skiing because skiing is really exciting. But in order to get the speed, the slide area must be tilted down, and when you slide to the bottom, you have to go uphill again or wait for the elevator to carry you. Michael wants to know the longest landslide in a region. A region is given by a two-dimensional array. Each number in the array represents the vertex height. The following is an example.
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
A person can slide from a certain point to one of the four adjacent points up and down, when and only when the height is reduced. In the preceding example, a slide is 24-17-16-1. Of course, 25-24-23-...-3-2-1 is longer. In fact, this is the longest one.
-
Input
-
The first row indicates the number of test data sets. The second row indicates the number of rows in the region R and the number of columns C (1 <= r, C <= 100 ). Below is the R row, each row has a C integer, representing the height H, 0 <= H <= 10000.
The following is the next group of data;
-
Output
-
The length of the maximum output area.
-
Sample Input
-
15 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
-
Sample output
-
25
-
Source
-
Typical questions
import java.util.Arrays;import java.util.Scanner;public class NYOJ10_ieayoio {public static int [][]map;public static int [][]f;public static int []dx={0,1,0,-1,0};public static int []dy={0,0,-1,0,1};public static void main(String[] args) {Scanner input=new Scanner(System.in);int t=input.nextInt();while (t-->0){int n=input.nextInt();int m=input.nextInt();map=new int [n+5][m+5];for (int i=0;i<map[0].length;i++)Arrays.fill(map[i], Integer.MAX_VALUE);f= new int [n+5][m+5];for (int i=0;i<f[0].length;i++)Arrays.fill(f[i], 1);for (int i=1;i<=n;i++)for (int j=1;j<=m;j++)map[i][j]=input.nextInt();for (int i=1;i<=n;i++)for (int j=1;j<=m;j++){dfs(i,j);}int max=Integer.MIN_VALUE;for (int i=1;i<=n;i++)for (int j=1;j<=m;j++){if (max<f[i][j]) max=f[i][j];}System.out.println(max);}}static int dfs(int x,int y){if (f[x][y]!=1) return f[x][y];if (isnoway(x,y)==false){return 1;}for (int i=1;i<=4;i++){int xx=x+dx[i];int yy=y+dy[i];if (map[x][y]>map[xx][yy]){int comway=1+dfs(xx,yy);if (f[x][y]<comway) f[x][y]=comway;}}return f[x][y];}static boolean isnoway(int x,int y){boolean flag=false;if (map[x][y]>map[x+dx[1]][y+dy[1]]) flag=true;if (map[x][y]>map[x+dx[2]][y+dy[2]]) flag=true;if (map[x][y]>map[x+dx[3]][y+dy[3]]) flag=true;if (map[x][y]>map[x+dx[4]][y+dy[4]]) flag=true;return flag;}}
I'm so happy that I have never been sure of my questions. After two days of practice, I did not expect to submit the questions once.
The method is deep search, but an array f [x] [Y] is added to save the longest distance from point (x, y) to the lowest point. DFS (x, y) used to search for this distance. If f [x] [Y] already exists, the function value is directly returned as f [x] [Y].
Isnoway function to determine whether a point (x, y) can slide to a lower point
Nyoj10, skiing