Creative Code

[2579번]계단 오르기 본문

백준 문제풀이

[2579번]계단 오르기

빛하루 2024. 5. 9. 17:54
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

int dp[301][3] = { 0 }; // n번째 계단에 m번연속 발판을 밟아 도착했을 때 얻을 수 있는 점수
int stair[301]; // 계단의 점수를 저장하는 배열

int solution(int N) {
	int answer = 0;
	dp[0][1] = 0;
	dp[0][2] = 0;
	dp[1][1] = stair[1];
	dp[1][2] = stair[1];
	for (int i = 2; i <= N; i++) {
		dp[i][1] = max(dp[i - 2][2] + stair[i],dp[i-2][1] + stair[i]); 
        // 1번연속 밟은 발판은 2칸 아래있는 곳에 2번연속 밟아서 얻은 점수와 2칸 아래있는 곳의 1번 연속 밟아서 얻은점수의 최댓값에 현재 계단의 점수를 더한것
		dp[i][2] = dp[i - 1][1] + stair[i];
        // 현재 계단에 2번연속 발판을 밟았을 때 바로 아래 칸의 점수 + 현재칸의점수
	}
	
	answer = max(dp[N][1], dp[N][2]); //마지막계단은 반드시 밟아야하므로 얻은 점수 중 최댓값
	return answer;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int N;
	cin >> N;
	int numStair = N;
	int index = 1;
	while (N > 0) {
		int k;
		cin >> k;
		stair[index] = k;
		index++;
		N--;
	}
	cout << solution(numStair);
}

 

 

http://https://www.acmicpc.net/problem/2579

 

'백준 문제풀이' 카테고리의 다른 글

[2312번]수 복원하기  (0) 2024.01.27
[2638번]치즈  (0) 2023.09.25
[1987번]알파벳  (0) 2023.09.25
[9251번]LCS  (0) 2023.09.25
[17144번]미세먼지 안녕!  (0) 2023.09.24