-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSkiing.java
More file actions
63 lines (59 loc) · 1.7 KB
/
Copy pathSkiing.java
File metadata and controls
63 lines (59 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package dp;
import java.util.Scanner;
public class Skiing {
static int[][] result;
static int max = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
double[][] a = new double[m][n];
result = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j] = sc.nextDouble();
result[i][j]=0;
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
result[i][j] = Calculate(a,m,n,i,j);
if(result[i][j]>max){
max = result[i][j];
}
}
}
System.out.println(max);
}
private static int Calculate(double[][] a,int m,int n, int i, int j) {
if(result[i][j]>0){
return result[i][j];
}
int te=0,x;
if(j-1>=0&&a[i][j]>a[i][j-1]){
x=Calculate(a,m,n,i,j-1);
if(x>te){
te =x;
}
}
if(j+1<n&&a[i][j]>a[i][j+1]){
x=Calculate(a,m,n,i,j+1);
if(x>te){
te=x;
}
}
if(i-1>=0&&a[i][j]>a[i-1][j]){
x=Calculate(a,m,n,i-1,j);
if(x>te){
te=x;
}
}
if(i+1<n&&a[i][j]>a[i+1][j]){
x=Calculate(a,m,n,i+1,j);
if(x>te){
te=x;
}
}
return te+1;
}
}