File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments