Skip to content

Commit 6877110

Browse files
authored
Merge pull request #1269 from AlgorithmWithGod/JHLEE325
[20251030] BOJ / G3 / 내리막 길 / 이준희
2 parents 099edd3 + ac4cbf3 commit 6877110

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int M, N;
7+
static int[][] map;
8+
static int[][] dp;
9+
static int[] dy = {-1, 1, 0, 0};
10+
static int[] dx = {0, 0, -1, 1};
11+
12+
public static void main(String[] args) throws Exception {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
16+
M = Integer.parseInt(st.nextToken());
17+
N = Integer.parseInt(st.nextToken());
18+
19+
map = new int[M][N];
20+
dp = new int[M][N];
21+
22+
for (int i = 0; i < M; i++) {
23+
st = new StringTokenizer(br.readLine());
24+
for (int j = 0; j < N; j++) {
25+
map[i][j] = Integer.parseInt(st.nextToken());
26+
dp[i][j] = -1;
27+
}
28+
}
29+
30+
System.out.println(dfs(0, 0));
31+
}
32+
33+
static int dfs(int y, int x) {
34+
if (y == M - 1 && x == N - 1) {
35+
return 1;
36+
}
37+
38+
if (dp[y][x] != -1) return dp[y][x];
39+
40+
dp[y][x] = 0;
41+
42+
for (int dir = 0; dir < 4; dir++) {
43+
int ny = y + dy[dir];
44+
int nx = x + dx[dir];
45+
46+
if (ny < 0 || nx < 0 || ny >= M || nx >= N) continue;
47+
if (map[ny][nx] < map[y][x]) {
48+
dp[y][x] += dfs(ny, nx);
49+
}
50+
}
51+
52+
return dp[y][x];
53+
}
54+
}
55+
```

0 commit comments

Comments
 (0)