슈콩

[BOJ] 백준 1932 정수 삼각형 본문

Algorithms/Baekjoon

[BOJ] 백준 1932 정수 삼각형

shukong 2025. 8. 23. 23:44

[문제] 

https://www.acmicpc.net/problem/1932

 

 

[소스 코드]

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));
    	StringTokenizer st;
    	int n = Integer.parseInt(br.readLine());
    	int[][] map = new int[n+1][n+1];
    	for(int i=1;i<=n;i++) {
    		st = new StringTokenizer(br.readLine());
    		for(int j=1;j<=i;j++) {
    			map[i][j] = Integer.parseInt(st.nextToken());
    		}
    	}
    	int[][] dp = new int[n+1][n+1];
    	dp[1][1] = map[1][1];
    	for(int i=2;i<=n;i++) {
    		for(int j=1;j<=i;j++) {
    			dp[i][j] = Math.max(dp[i-1][j-1], dp[i-1][j]) + map[i][j];
    		}
    	}
    	int max = 0;
    	for(int i=1;i<=n;i++) {
    		max = Math.max(max, dp[n][i]);
    	}
    	System.out.println(max);
    }
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 2133 타일 채우기  (0) 2025.08.24
[BOJ] 백준 2011 암호코드  (0) 2025.08.24
[BOJ] 백준 1915 가장 큰 정사각형  (0) 2025.08.23
[BOJ] 백준 1912 연속합  (0) 2025.08.23
[BOJ] 백준 1904 01타일  (0) 2025.08.23