forked from jxixeun/jxix_java_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1520.java
More file actions
49 lines (45 loc) · 1.46 KB
/
Copy pathBOJ1520.java
File metadata and controls
49 lines (45 loc) · 1.46 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
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ1520 {
static int N, M;
static int[][] map;
static int[][] dp;
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int dfs(int x, int y) {
dp[x][y] = 0;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < M && ny < N && map[x][y] > map[nx][ny]) {
if (dp[nx][ny] == -1) {
dp[x][y] += dfs(nx, ny);
} else {
dp[x][y] += dp[nx][ny];
}
}
}
return dp[x][y];
}
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());
map = 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++) {
map[i][j] = Integer.parseInt(st.nextToken());
dp[i][j] = -1;
}
}
dp[M - 1][N - 1] = 1;
dfs(0, 0);
System.out.println(dp[0][0]);
}
}