문제) 백준 - 동적 계획법 - 징검다리 건너기
https://www.acmicpc.net/problem/21317
21317번: 징검다리 건너기
산삼을 얻기 위해 필요한 영재의 최소 에너지를 출력한다.
www.acmicpc.net
전형적인 동적 계획법으로 쉽게 해결할 수 있는 문제였습니다. solve(i, usedMegajump)를 통해 i 번째 돌까지 갔을때 필요한 최소 에너지의 memoization을 통해 해결합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int solve(int idx, int usedMegaJump){ | |
if(idx == N) return 0; | |
if(idx > N) return INF; | |
int &ret = cache[idx][usedMegaJump]; | |
if(ret != -1) return ret; | |
ret = INF; | |
// Small Jump | |
ret = min(ret, solve(idx + 1, usedMegaJump) + jumpEnergy[idx].first); | |
// Big Jump | |
ret = min(ret, solve(idx + 2, usedMegaJump) + jumpEnergy[idx].second); | |
// Mega Jump | |
if(usedMegaJump == 0) | |
ret = min(ret, solve(idx + 3, 1) + K); | |
return ret; | |
} |
Full Code)
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 21758번 - 꿀 따기 (C++) 문제 및 풀이 (0) | 2022.02.24 |
---|---|
[백준] 11655번 - ROT13 (C++) 문제 및 풀이 (0) | 2022.02.24 |
[백준] 20495번 - 수열과 헌팅 (C++) 문제 및 풀이 (0) | 2022.02.23 |
[백준] 6996번 - 애너그램 (C++) 문제 및 풀이 (0) | 2022.02.23 |
[백준] 20294번 - 트리의 기둥과 가지 (C++) 문제 및 풀이 (0) | 2022.02.22 |
댓글