쵼쥬 2021. 10. 19. 21:38


DP 를 이용해서 입력 받을 때 바로 그 위치의 최댓값을 구해주었다.

 

내 코드

package com.company;

import java.io.*;
import java.util.*;

public class Main {
    static int N, M;
    static int[][] array;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        array = new int[N + 1][M + 1];


        for (int i = 1; i <= N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= M; j++) {
                int x = Integer.parseInt(st.nextToken());
                array[i][j] = Math.max(array[i - 1][j], Math.max(array[i][j - 1], array[i - 1][j - 1])) + x;
            }
        }

        System.out.println(array[N][M]);
    }
}