Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions JHLEE325/202510/30 BOJ G3 내리막 길.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```java
import java.io.*;
import java.util.*;

public class Main {
static int M, N;
static int[][] map;
static int[][] dp;
static int[] dy = {-1, 1, 0, 0};
static int[] dx = {0, 0, -1, 1};

public static void main(String[] args) throws Exception {
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;
}
}

System.out.println(dfs(0, 0));
}

static int dfs(int y, int x) {
if (y == M - 1 && x == N - 1) {
return 1;
}

if (dp[y][x] != -1) return dp[y][x];

dp[y][x] = 0;

for (int dir = 0; dir < 4; dir++) {
int ny = y + dy[dir];
int nx = x + dx[dir];

if (ny < 0 || nx < 0 || ny >= M || nx >= N) continue;
if (map[ny][nx] < map[y][x]) {
dp[y][x] += dfs(ny, nx);
}
}

return dp[y][x];
}
}
```