알고리즘/백준 BOJ
[ BOJ/C# ] 2579 계단 오르기
왹져박사
2023. 10. 7. 02:15
https://www.acmicpc.net/problem/2579
2579번: 계단 오르기
계단 오르기 게임은 계단 아래 시작점부터 계단 꼭대기에 위치한 도착점까지 가는 게임이다. <그림 1>과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점
www.acmicpc.net
DP 문제 중에서도 꽤나 헷갈렸던 문제이다.
경우의 수를 따져 최댓값을 구하였다. 조건들을 정확히 파악하는 것이 중요하다.
n==1일 때와 n==2일 때의 조건을 정확히 나누지 않아 초반에 인덱스 에러가 났다..
using System;
using System.IO;
namespace B2579
{
class Program
{
static void Main()
{
StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
int n = int.Parse(sr.ReadLine());
int[] stairs = new int[n + 1];
int[] results = new int[n + 1];
for(int i = 1; i <= n; i++) stairs[i] = int.Parse(sr.ReadLine());
results[0] = 0;
if(n>=1) results[1] = stairs[1];
if(n>=2) results[2] = stairs[1] + stairs[2];
if (n >= 3)
{
for (int i = 3; i <= n; i++)
{
results[i] = Math.Max(
stairs[i] + stairs[i - 1] + results[i - 3], stairs[i] + results[i - 2]);
}
}
sw.Write(results[n]);
sr.Close();
sw.Close();
}
}
}