728x90
정수 삼각형
https://www.acmicpc.net/problem/1932
1932번: 정수 삼각형
첫째 줄에 삼각형의 크기 n(1 ≤ n ≤ 500)이 주어지고, 둘째 줄부터 n+1번째 줄까지 정수 삼각형이 주어진다.
www.acmicpc.net
풀이
import annotation.boj.BOJ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
@BOJ
public class 백준1932 {
private static int[][] dp;
private static int[][] triangle;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
dp = new int[N+1][N+1];
triangle = new int[N+1][N+1];
for (int i = 1; i <= N; i++) {
final int[] ints = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
for (int j = 1; j<= ints.length; j++) {
triangle[i][j]=ints[j-1];
if(i==N) {
dp[i][j] = ints[j - 1];
}
}
}
for (int i = N-1; i > 0; i--) {
for (int j = 1; j <= i; j++) {
dp[i][j] = triangle[i][j]+Math.max(dp[i+1][j], dp[i+1][j+1]);
}
}
System.out.println(dp[1][1]);
}
}
728x90
'Algorithm > 백준' 카테고리의 다른 글
[백준/11053/Java] 가장 긴 증가하는 부분 수열 (0) | 2022.02.21 |
---|---|
[백준/10844/Java] 쉬운 계단 수 (0) | 2022.02.20 |
[백준/9184/Java] 신나는 함수 실행 (0) | 2022.02.18 |
[백준/2110/Java] 공유기 설치 (0) | 2022.02.16 |
[백준/2805/Java] 나무 자르기 (0) | 2022.02.15 |