📢 공지합니다
이 게시글은 메인 페이지에 항상 고정되어 표시됩니다.
https://www.acmicpc.net/problem/2169
package dp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class DP2169 {
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[][] map = new int[N][M];
int[][] temp = new int[2][M];
int[][] dp = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (i == 0) {
if (j == 0) {
dp[i][j] = map[i][j];
} else {
dp[i][j] = dp[i][j - 1] + map[i][j];
}
} else {
if (j == 0) {
temp[0][j] = dp[i - 1][j] + map[i][j];
temp[1][M - 1 - j] = dp[i - 1][M - 1] + map[i][M - 1];
} else {
temp[0][j] = Math.max(dp[i - 1][j], temp[0][j - 1]) + map[i][j];
temp[1][M - 1 - j] = Math.max(dp[i - 1][M - 1 - j], temp[1][M - j]) + map[i][M - 1 - j];
}
}
}
if (i > 0) {
for (int k = 0; k < M; k++) {
dp[i][k] = Math.max(temp[0][k], temp[1][k]);
}
}
}
System.out.println(dp[N - 1][M - 1]);
}
}
DP 역사상 최고로 어려운 문제였다! OO 코테에서 유사한 유형이 출제돼서, 복습 차원에서 직접 풀어봤다.
[백준 11052번] 카드 구매하기 (0) | 2025.03.25 |
---|---|
[백준 2156번] 포도주 시식 (0) | 2025.03.25 |
[백준 15666번] N과 M(12) (0) | 2025.03.23 |
[백준 2877번] 4와 7 (0) | 2025.03.19 |
[백준 17070번] 파이프 옮기기 1 (0) | 2025.03.18 |