[20260117] BOJ / G5 / 알약 / 한종욱 #1799
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://www.acmicpc.net/problem/4811
🧭 풀이 시간
60분
👀 체감 난이도
✏️ 문제 설명
알약이 N개가 있다. 반 개를 먹거나 한 개를 먹는다. 반 개를 먹을 땐 H, 한 개를 먹을 땐 W를 적는다. 2N일을 먹는다고 가정할 때 가능한 문자열의 개수는?
🔍 풀이 방법
상태 정의 ($dp[w][h]$ )
점화식
현재 상태에서 할 수 있는 행동은 두 가지뿐이다. 이 두 행동의 결과를 더하면 현재의 경우의 수가 된다.
dp[w-1][h+1]에서 값을 가져옴dp[w][h-1]에서 값을 가져옴(단, $w>0, h>0$일 때 유효)
초기화
1로 초기화한 뒤 Bottom-Up으로 계산한다.⏳ 회고
처음에는 DP의 상태 전이 방향이 헷갈려서 고생했다.
$i+1, j-1$ )에서 현재로 오는 것"으로 착각하여 인덱스를 반대로 생각했었다.
dp[i][j]를 구할 때 "과거의 값(하지만 Bottom-Up 방식의 핵심은 "미래의 결과(W를 먹은 후, H를 먹은 후)를 미리 계산해두고, 현재 상태가 그 값들을 가져와서 합치는 것"임을 깨달았다.