-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1520.java
More file actions
68 lines (57 loc) · 1.73 KB
/
1520.java
File metadata and controls
68 lines (57 loc) · 1.73 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
64
65
66
67
68
import java.io.*;
import java.util.*;
public class Main {
public static int M;
public static int N;
public static int[][] adj;
public static int[] dx = {1, 0, -1, 0};
public static int[] dy= {0,1,0,-1};
public static int[][] dp;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
adj = new int[M][N];
dp = new int[M][N];
for(int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for(int j =0; j < N; j++) {
adj[i][j] = Integer.parseInt(st.nextToken());
dp[i][j] = -1;
}
}
int answer = dfs(0, 0);
// for(int i = 0; i < M; i++) {
// for(int j =0; j < N; j++) {
// System.out.printf("%d ", dp[i][j]);
// }
// System.out.println();
// }
System.out.println(answer);
}
public static boolean rangeCheck(int x, int y){
if((x>=0&&x<M) &&(y>=0&&y<N)){
return true;
} else{
return false;
}
}
public static int dfs(int x,int y){
if (x == M-1 && y==N-1){
return 1;
}
if (dp[x][y]!=-1){
return dp[x][y];
}
dp[x][y] = 0;
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(rangeCheck(nx, ny) && adj[nx][ny]<adj[x][y]) {
dp[x][y] += dfs(nx,ny);
}
}
return dp[x][y];
}
}