|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/* |
| 5 | + * 로봇 조종하기 |
| 6 | + */ |
| 7 | + |
| 8 | +public class DH_2169 { |
| 9 | + static int INF = - 1_000 * 1_000 * 10; |
| 10 | + |
| 11 | + public static void main(String[] args) throws Exception { |
| 12 | + System.setIn(new FileInputStream("./input/BOJ2169.txt")); |
| 13 | + |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + |
| 17 | + int N = Integer.parseInt(st.nextToken()); |
| 18 | + int M = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + int[][] arr = new int[N + 1][M + 1]; |
| 21 | + int[][][] dp = new int[3][N + 1][M + 1]; |
| 22 | + |
| 23 | + for(int k = 0; k < 3; k++) { |
| 24 | + for(int r = 0; r < dp[0].length; r++) Arrays.fill(dp[k][r], INF); |
| 25 | + } |
| 26 | + for(int r = 1; r < arr.length; r++) { |
| 27 | + st = new StringTokenizer(br.readLine()); |
| 28 | + for(int c = 1; c < arr[0].length; c++) { |
| 29 | + arr[r][c] = Integer.parseInt(st.nextToken()); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // dp 테이블 채우기 |
| 34 | + for(int r = 1; r < dp[0].length; r++) { |
| 35 | + if(r == 1) { |
| 36 | + dp[0][r][0] = 0; |
| 37 | + for(int c = 1; c < dp[0][0].length; c++) { |
| 38 | + dp[0][r][c] = arr[r][c] + dp[0][r][c - 1]; |
| 39 | + } |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + // 위에서 오는 경우 |
| 44 | + for(int c = 1; c < dp[0][0].length; c++) { |
| 45 | + dp[1][r][c] = Math.max(dp[0][r - 1][c], Math.max(dp[1][r - 1][c], dp[2][r - 1][c])) + arr[r][c]; |
| 46 | + } |
| 47 | + |
| 48 | + // 왼쪽에서 오는 경우 |
| 49 | + for(int c = 1; c < dp[0][0].length; c++) { |
| 50 | + dp[0][r][c] = Math.max(dp[0][r][c - 1], dp[1][r][c - 1]) + arr[r][c]; |
| 51 | + } |
| 52 | + |
| 53 | + // 오른쪽에서 오는 경우 |
| 54 | + for(int c = dp[0][0].length - 2; c > 0; c--) { |
| 55 | + dp[2][r][c] = Math.max(dp[1][r][c + 1], dp[2][r][c + 1]) + arr[r][c]; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + } |
| 60 | + System.out.println(Math.max(dp[0][N][M], Math.max(dp[1][N][M], dp[2][N][M]))); |
| 61 | + } |
| 62 | +} |
0 commit comments