|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static int height,width; |
| 8 | + static int[][] arr; |
| 9 | + static int[][] dp; |
| 10 | + static int[] dy = {-1,0,1,0}; |
| 11 | + static int[] dx = {0,1,0,-1}; |
| 12 | + |
| 13 | + |
| 14 | + public static void main(String[] args) throws Exception { |
| 15 | + init(); |
| 16 | + process(); |
| 17 | + print(); |
| 18 | + |
| 19 | + } |
| 20 | + |
| 21 | + public static void init() throws IOException { |
| 22 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 23 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 24 | + height = Integer.parseInt(st.nextToken()); |
| 25 | + width = Integer.parseInt(st.nextToken()); |
| 26 | + arr = new int[height][width]; |
| 27 | + dp = new int[height][width]; |
| 28 | + |
| 29 | + for (int i = 0; i < height; i++) { |
| 30 | + Arrays.fill(dp[i],-1); |
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + for (int j = 0; j < width; j++) { |
| 33 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + public static void process(){ |
| 41 | + dfs(0,0); |
| 42 | + } |
| 43 | + |
| 44 | + public static void print(){ |
| 45 | + |
| 46 | + System.out.println(dp[0][0]); |
| 47 | + } |
| 48 | + |
| 49 | + public static int dfs(int y, int x){ |
| 50 | + if ( y == height - 1 && x == width - 1 ) { |
| 51 | + return 1; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + if(dp[y][x] != -1) return dp[y][x]; |
| 56 | + |
| 57 | + dp[y][x] = 0; |
| 58 | + |
| 59 | + for (int i = 0; i < 4; i++){ |
| 60 | + int ny = y + dy[i]; |
| 61 | + int nx = x + dx[i]; |
| 62 | + if (ny < 0 || ny >= height || nx < 0 || nx >= width) continue; |
| 63 | + if (arr[ny][nx] >= arr[y][x]) continue; |
| 64 | + |
| 65 | + dp[y][x] += dfs(ny, nx); |
| 66 | + } |
| 67 | + |
| 68 | + return dp[y][x]; |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +``` |
0 commit comments