-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_2169.java
More file actions
57 lines (44 loc) · 1.95 KB
/
Copy pathBOJ_2169.java
File metadata and controls
57 lines (44 loc) · 1.95 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
48
49
50
51
52
53
54
55
56
57
import java.io.*;
import java.util.*;
public class BOJ_2169 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] arr = new int[N][M];
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
// System.out.println("arr = " + Arrays.deepToString(arr));
int[][] answer = new int[N][M];
answer[0][0] = arr[0][0];
for(int i = 1; i < M; i++) {
answer[0][i] = answer[0][i-1] + arr[0][i];
}
for(int i = 1; i < N; i++) {
int[][] temp = new int[2][M];
// 왼쪽에서 오른쪽으로 이동하는데 위에서랑 비교
temp[0][0] = answer[i-1][0] + arr[i][0];
for (int j = 1; j < M; j++) {
temp[0][j] = Math.max(temp[0][j-1], answer[i-1][j]) + arr[i][j];
}
// System.out.println("1) temp = " + Arrays.deepToString(temp));
// 오른쪽에서 왼쪽으로 이동하는데 위에서랑 비교
temp[1][M-1] = answer[i-1][M-1] + arr[i][M-1];
for (int j = M-2; j >= 0; j--) {
temp[1][j] = Math.max(temp[1][j+1], answer[i-1][j]) + arr[i][j];
}
// System.out.println("2) temp = " + Arrays.deepToString(temp));
for (int j = 0; j < M; j++) {
answer[i][j] = Math.max(temp[0][j], temp[1][j]);
}
// System.out.println("answer = " + Arrays.deepToString(answer));
}
// System.out.println("answer = " + Arrays.deepToString(answer));
System.out.println(answer[N-1][M-1]);
}
}