#DevStudy/문제풀이

[Baekjoon] 2579 - 계단오르기

검은_백조 2016. 10. 11. 19:28



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



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
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
 
 
int stairCount;
int stairScore[300];
 
int totalScore[300];
 
int max(int a, int b)
{
    return a > b ? a : b;
}
 
int main() {
    scanf("%d\n"&stairCount);
    
    for(int i = 0; i < stairCount; i++){
        scanf("%d"&stairScore[i]);
    }
    
    totalScore[0= stairScore[0];
    totalScore[1= totalScore[0+ stairScore[1];
    totalScore[2= max(stairScore[0], stairScore[1]) + stairScore[2];
    
    for(int i = 3; i < stairCount; i++){
        totalScore[i] = max(totalScore[i - 3+ stairScore[i - 1], totalScore[i - 2]) + stairScore[i];
    }
    
    printf("%d\n", totalScore[stairCount-1]);
    
    return 1;
}
cs




i 번째 계단으로 올라가는 경우의 수는


i - 3 , i - 1, i 번째 계단을 밟거나

i - ? , i - 2, i 번째 계단을 밟는 것이 존재한다.


처음 0, 1, 2번째 계단에 대한 값을 구한 후

3번째 계단 부터는 이 공식에 따라 점수를 비교하고 최대값을 찾는다.