개발자 톡

연습문제 톡 나무 수확

12번 테케만 틀리는데 반례가 있을까요...?

등록일
2024-02-01 00:34:47
조회수
264
작성자
mont4857
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main {
    static int[][] map;
    static int[][] maxValue;


    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        map = new int[n + 1][n + 1];
        maxValue = new int[n + 1][n + 1];
        StringTokenizer st;
        for (int i = 1; i <= n; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= n; j++) {
                int value = Integer.parseInt(st.nextToken());
                if (i == 1 && j == 1) {
                    map[i][j] = value * 2;
                    maxValue[i][j] = value;
                    continue;
                }
                int up, left;
                int upMax, leftMax;
                if (value > maxValue[i - 1][j]) {
                    up = map[i - 1][j] - maxValue[i - 1][j] + (2 * value);
                    upMax = value;
                } else {
                    up = map[i - 1][j] + value;
                    upMax = maxValue[i-1][j];
                }
                if (value > maxValue[i][j - 1]) {
                    left = map[i][j - 1] - maxValue[i][j - 1] + (2 * value);
                    leftMax = value;
                } else {
                    left = map[i][j - 1] + value;
                    leftMax = maxValue[i][j-1];
                }


                if (up > left) {
                    map[i][j] = up;
                    maxValue[i][j] = upMax;
                } else if (up < left) {
                    map[i][j] = left;
                    maxValue[i][j] = leftMax;
                } else {
                    map[i][j] = up;
                    maxValue[i][j] = Math.max(upMax, leftMax);
                }
            }


        }
        System.out.println(map[n][n]);
    }
}


up, left에 해당하는 곳에 도착했을때 최대 값을 더한 값을 넣었습니다.

12번만 안되는 건데 왜 안되는 건지 모르겠습니다.. ㅠㅠ




#나무_수확

이 카테고리의 톡 더보기