Home BOJ. Down Hill (1520)
Post
Cancel

BOJ. Down Hill (1520)

[Link] https://www.acmicpc.net/problem/1520


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
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <stack>
#include <cmath>
// #include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define command_param int argc, char *argv[]
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "

int hmap[500][500];
ll dp[500][500];
bool visit[500][500];
int di[4] = { -1, 0, 1, 0 };
int dj[4] = { 0, 1, 0, -1 };
int n, m;

ll dfs(int ci, int cj) {
  if(dp[ci][cj] != 0) return dp[ci][cj];
  if(ci == n-1 && cj == m-1) return dp[ci][cj] = 1;
  for0(k, 4) {
    int ai = ci + di[k], aj = cj + dj[k];
    if(ai < 0 || aj < 0 || ai >= n || aj >= m || hmap[ci][cj] <= hmap[ai][aj]) continue;
    if(dfs(ai, aj) == -1) continue;
    dp[ci][cj] += dfs(ai, aj);
  }
  if(dp[ci][cj] == 0) dp[ci][cj] = -1;
  return dp[ci][cj];
}

int main() {
  FASTIO;
  cin >> n >> m;
  for0(i, n) for0(j, m) cin >> hmap[i][j];
  int cnt = 0;
  cout << (dfs(0, 0) == -1 ? 0 : dfs(0, 0));
  return 0;
}
This post is licensed under CC BY 4.0 by the author.