개발자 톡

연습문제 톡 나무 수확

자바코드 정답입니다.

등록일
2024-07-05 17:30:38
조회수
46
작성자
rudtnrdid123
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader br = new  BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[][] tree =  new int[n][n];
        
        for(int i = 0; i<n; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            for(int j =  0; j<n; j++){
                tree[i][j] =  Integer.parseInt(st.nextToken());
            }
        }
        int[][] sum =  new int[n][n];
        int[][] answer =  new int[n][n];

        sum[0][0] = tree[0][0];
        answer[0][0] = tree[0][0]*2;
        
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++){
                if(i>0){
                    sum[i][j] =  Math.max(sum[i][j],sum[i-1][j]+tree[i][j]);
                    answer[i][j] = Math.max(answer[i][j],answer[i-1][j] + tree[i][j]);
                    answer[i][j] = Math.max(answer[i][j],sum[i-1][j]+tree[i][j]*2);
                }
                if(j>0){
                    sum[i][j] =  Math.max(sum[i][j],sum[i][j-1]+tree[i][j]);
                    answer[i][j] = Math.max(answer[i][j],answer[i][j-1] + tree[i][j]);
                    answer[i][j] = Math.max(answer[i][j],sum[i][j-1]+tree[i][j]*2);
                }
            }
        }
        System.out.println(answer[n-1][n-1]);
    }
}


#나무_수확

이 카테고리의 톡 더보기