-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1520_내리막_길.cpp
50 lines (40 loc) · 952 Bytes
/
1520_내리막_길.cpp
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
50
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, M;
int arr[505][505];
ll dp[505][505];
int darr[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
ll solve(int x, int y)
{
if (x == M - 1 && y == N - 1)
return 1;
if (dp[y][x] != -1)
return dp[y][x];
ll &retval = dp[y][x];
retval = 0;
for (auto &[dx, dy] : darr)
{
int nx = x + dx, ny = y + dy;
if (nx < 0 || nx >= M || ny < 0 || ny >= N)
continue;
if (arr[y][x] <= arr[ny][nx])
continue;
retval += solve(nx, ny);
}
return retval;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
dp[i][j] = -1;
}
}
cout << solve(0, 0) << '\n';
return 0;
}