|
| 1 | +```java |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.StringTokenizer; |
| 8 | + |
| 9 | +public class Main { |
| 10 | + |
| 11 | + static int height,width,ans; |
| 12 | + |
| 13 | + static int[][] arr; |
| 14 | + static int[][][] dp; |
| 15 | + |
| 16 | + static final int TOP = 0; |
| 17 | + static final int LEFT = 1; |
| 18 | + static final int RIGHT = 2; |
| 19 | + static final int MIN = Integer.MIN_VALUE/2; |
| 20 | + |
| 21 | + public static void main(String[] args) throws IOException { |
| 22 | + init(); |
| 23 | + process(); |
| 24 | + print(); |
| 25 | + } |
| 26 | + |
| 27 | + private static void init() throws IOException { |
| 28 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 29 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 30 | + height = Integer.parseInt(st.nextToken()); |
| 31 | + width = Integer.parseInt(st.nextToken()); |
| 32 | + arr = new int[height][width]; |
| 33 | + dp = new int[height][width][3]; |
| 34 | + ans = MIN; |
| 35 | + // 합의 최댓값을 직전에 어느 방향에서 왔는지에 따라 구분. |
| 36 | + // 0 ,1 , 2가 순서대로 위,왼,오 |
| 37 | + |
| 38 | + for (int i = 0; i < height; i++){ |
| 39 | + st = new StringTokenizer(br.readLine()); |
| 40 | + for (int j = 0; j < width; j++){ |
| 41 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 42 | + Arrays.fill(dp[i][j], MIN); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + private static void process() throws IOException { |
| 49 | + calculateFirstLine(); |
| 50 | + for (int i = 1; i < height; i++){ |
| 51 | + calculateFromTop(i); |
| 52 | + calculateFromLeft(i); |
| 53 | + calculateFromRight(i); |
| 54 | + } |
| 55 | + |
| 56 | + for (int i = 0; i < 3; i++){ |
| 57 | + ans = Math.max (ans,dp[height-1][width-1][i]); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + private static void calculateFromRight(int curHeight) { |
| 63 | + dp[curHeight][width-1][RIGHT] = dp[curHeight][width-1][TOP]; |
| 64 | + for (int i = width - 2; i >= 0; i--) { |
| 65 | + dp[curHeight][i][RIGHT] = arr[curHeight][i] + Math.max(dp[curHeight][i+1][TOP], dp[curHeight][i+1][RIGHT]); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void calculateFromLeft(int curHeight) { |
| 70 | + dp[curHeight][0][LEFT] = dp[curHeight][0][TOP]; |
| 71 | + for (int i = 1; i < width; i++){ |
| 72 | + dp[curHeight][i][LEFT] = arr[curHeight][i] + Math.max(dp[curHeight][i-1][TOP], dp[curHeight][i-1][LEFT]) ; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static void calculateFromTop(int curHeight) { |
| 77 | + for (int i = 0; i < width; i++){ |
| 78 | + int curLargest = MIN; |
| 79 | + for (int j = 0; j < 3; j++) curLargest = Math.max(curLargest, dp[curHeight-1][i][j]); |
| 80 | + |
| 81 | + dp[curHeight][i][TOP] = curLargest + arr[curHeight][i]; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static void calculateFirstLine() { |
| 86 | + int cur = 0; |
| 87 | + for (int i = 0; i < width; i++){ |
| 88 | + cur += arr[0][i]; |
| 89 | + dp[0][i][0] = cur; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + private static void print() { |
| 95 | + System.out.println(ans); |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
0 commit comments