-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_11048.cpp
More file actions
42 lines (35 loc) · 870 Bytes
/
BOJ_11048.cpp
File metadata and controls
42 lines (35 loc) · 870 Bytes
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
// 22 / 1 / 17
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m;
vector<vector<int>> matrix;
vector<vector<int>> dp;
vector<pair<int, int>> dir = {{0, -1}, {-1, 0}, {-1, -1}};
int search(int r, int c)
{
if ((r < 0) || (c < 0))
return 0;
if (dp[r][c] != -1)
return dp[r][c];
vector<int> arr;
for (int i = 0; i < 3; i++)
arr.push_back(search(r + dir[i].first, c + dir[i].second));
return dp[r][c] = *max_element(arr.begin(), arr.end()) + matrix[r][c];
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
matrix.assign(n, vector<int>(m, 0));
dp.assign(n, vector<int>(m, -1));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cin >> matrix[i][j];
}
cout << search(n - 1, m - 1);
return 0;
}