Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- qorwns
- 백준 17822
- 스택의 특징
- 조세퍼스 순열
- 별 찍기 10
- 백준 1158
- 백준
- 시간 복잡도
- 버킷 정렬
- 풀이
- heap
- dfs
- 백준 5397
- 백준 1406
- 백준 17779
- 백준 2447
- c#
- C/C++ 구현
- 자료구조
- AVL 시간 복잡도
- 1764
- Stack 이란
- 게리멘더링2
- 해시 구현
- 원판 돌리기
- 구현
- ㅣ풀이
- 백준 17471
- 해시구현
- 5397
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 1932] 정수 삼각형 본문
분류 : dp
요구사항
꼭대기에서 시작하여 끝까지 가는 경로 중 각 노드에 적힌 값이 최대인 값을 구하여라
풀이
dp를 사용하여 top-down형식으로 구하였다
점화식은
ref = map[floor][now] + max(func(floor - 1, now - 1), func(floor - 1, now));
현재 위치에서의 최대값은 (현재층 -1)층의 왼쪽, 오른쪽 중 최대값을 더한 값이다
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
int d[550][550];
int map[550][550];
int n;
int func(int floor, int now) {
if (floor == 1) {
return map[1][1];
}
//cout << floor << " " << now << endl;
int& ref = d[floor][now];
if (ref != -1) {
return ref;
}
if (now == 1) {
ref = map[floor][now] + func(floor - 1, now);
}
else if (now == floor) {
ref = map[floor][now] + func(floor - 1, now - 1);
}
else
ref = map[floor][now] + max(func(floor - 1, now - 1), func(floor - 1, now));
return ref;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
scanf("%d", &map[i][j]);
d[i][j] = -1;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, func(n, i));
// cout << ans << endl;
}
cout << ans << endl;
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 3108] 로고 (0) | 2020.04.11 |
---|---|
[백준 1912] 연속합 (0) | 2020.04.09 |
[백준 2579] 계단 오르기 (0) | 2020.04.09 |
[백준 1981] 배열에서 이동 (0) | 2020.04.04 |
[백준 2022] 사다리 (0) | 2020.04.04 |
Comments