forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.java
50 lines (47 loc) Β· 1.75 KB
/
1.java
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
import java.util.*;
public class Main {
static int testCase, n, m;
static int[][] arr = new int[20][20];
static int[][] dp = new int[20][20];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// ν
μ€νΈ μΌμ΄μ€(Test Case) μ
λ ₯
testCase = sc.nextInt();
for (int tc = 0; tc < testCase; tc++) {
// κΈκ΄ μ 보 μ
λ ₯
n = sc.nextInt();
m = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = sc.nextInt();
}
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°μ μν 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] + Math.max(leftUp, Math.max(leftDown, left));
}
}
int result = 0;
for (int i = 0; i < n; i++) {
result = Math.max(result, dp[i][m - 1]);
}
System.out.println(result);
}
}
}