forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1.cpp
More file actions
47 lines (44 loc) Β· 1.41 KB
/
1.cpp
File metadata and controls
47 lines (44 loc) Β· 1.41 KB
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
#include <bits/stdc++.h>
using namespace std;
int testCase, n, m;
int arr[20][20];
int dp[20][20];
int main(void) {
// ν
μ€νΈ μΌμ΄μ€(Test Case) μ
λ ₯
cin >> testCase;
for (int tc = 0; tc < testCase; tc++) {
// κΈκ΄ μ 보 μ
λ ₯
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°μ μν 2μ°¨μ DP ν
μ΄λΈ μ΄κΈ°ν
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dp[i][j] = arr[i][j];
}
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ° μ§ν
for (int j = 1; j < m; j++) {
for (int i = 0; i < n; i++) {
int leftUp, leftDown, left;
// μΌμͺ½ μμμ μ€λ κ²½μ°
if (i == 0) leftUp = 0;
else leftUp = dp[i - 1][j - 1];
// μΌμͺ½ μλμμ μ€λ κ²½μ°
if (i == n - 1) leftDown = 0;
else leftDown = dp[i + 1][j - 1];
// μΌμͺ½μμ μ€λ κ²½μ°
left = dp[i][j - 1];
dp[i][j] = dp[i][j] + max(leftUp, max(leftDown, left));
}
}
int result = 0;
for (int i = 0; i < n; i++) {
result = max(result, dp[i][m - 1]);
}
cout << result << '\n';
}
}