문제) 백준 - 동적 계획법(Dynamic Programming) - 알약
https://www.acmicpc.net/problem/4811
4811번: 알약
입력은 최대 1000개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄이며, 병에 들어있는 약의 개수 N ≤ 30 가 주어진다. 입력의 마지막 줄에는 0이 하나 주어진다.
www.acmicpc.net
cache[w][h]: 약 w(한 조각), h(반 조각)의 개수 memoization
solve(w, h): top-down방식으로 알약 한 조각 먹을수 있는 경우(w > 0), 반 조각 먹을 수 있는 경우(h > 0)를 더해서 return
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 cache[MAX][MAX]; | |
int solve(int w, int h) { | |
//기저 사례 | |
if (w == 0) return 1; | |
int& ret = cache[w][h]; | |
if (ret != -1) return ret; | |
ret = 0; | |
if (w > 0) | |
ret += solve(w - 1, h + 1); | |
if (h > 0) | |
ret += solve(w, h - 1); | |
return ret; | |
} |
Full code)
GitHub - Chocochip101/BOJ_Solution
Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 2623번 - 음악프로그램 (C++) 문제 및 풀이 (0) | 2021.10.05 |
---|---|
[백준] 2056번 - 작업 (C++) 문제 및 풀이 (0) | 2021.10.01 |
[백준] 20040번 - 사이클 게임 (C++) 문제 및 풀이 (0) | 2021.09.28 |
[백준] 11653번 - 소인수 분해 (C++) 문제 및 풀이 (0) | 2021.09.27 |
[백준] 14881번 - 물통 문제 (C++) 문제 및 풀이 (0) | 2021.09.24 |
댓글