[문제]
https://www.acmicpc.net/problem/2579
2579번: 계단 오르기
계단 오르기 게임은 계단 아래 시작점부터 계단 꼭대기에 위치한 도착점까지 가는 게임이다. <그림 1>과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점
www.acmicpc.net
[풀이]
다이나믹 프로그래밍
[코드]
#include <iostream>
using namespace std;
int s[305];
int n;
int d[305][3];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i=1; i<=n; i++) cin >> s[i];
if(n==1){
cout << s[1];
return 0;
}
d[1][1] = s[1];
d[1][2] = 0;
d[2][1] = s[2];
d[2][2] = s[1]+s[2];
for(int i=3; i<=n; i++){
d[i][1] = max(d[i-2][1], d[i-2][2]) + s[i];
d[i][2] = d[i-1][1] + s[i];
}
cout << max(d[n][1], d[n][2]);
}
'알고리즘 > 풀이' 카테고리의 다른 글
백준 11652번: 카드 - C++ (0) | 2022.02.13 |
---|---|
백준 9095번: 1, 2, 3 더하기 - C++ (1) | 2022.02.13 |
백준 11728번 - 배열 합치기 C++ (0) | 2022.02.06 |
백준 2751번 - 수 정렬하기 2 C++ (0) | 2022.02.06 |
백준 15686 - 치킨 배달 / C++ (0) | 2022.01.23 |