-문제:https://www.acmicpc.net/problem/9184
도전히 인자 3개인 dp를 못 구할 것 같았는데 3중 리스트를 만들면 된다는 걸 깨달았다.
그러고 함수 w를 생성할 생각은 못했다.
그래도 일단 자료구조라도 생각해낸게 발전한 것 같다
-정답풀이:
dp=[[[0]*21 for _ in range(21)] for _ in range(21)]
def w(a,b,c):
if a<=0 or b<=0 or c<=0:
return 1
if a>20 or b>20 or c>20:
return w(20,20,20)
if dp[a][b][c]: #여기
return dp[a][b][c]
elif a<b<c:
dp[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c)
return dp[a][b][c]
else:
dp[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1)
return dp[a][b][c]
while True:
a,b,c=map(int,input().split())
if a==-1 and b==-1 and c==-1:
break
print(f'w({a}, {b}, {c}) = {w(a,b,c)}')
-시도해본 풀이
while True:
a,b,c=map(int,input().split())
if a==-1 and b==-1 and c==-1:
break
if a<=0 or b<=0 or c<=0:
dp[a][b][c]=1
if a>20 or b>20 or c>20:
dp[a][b][c]=dp[20][20][20]
if a<b<c:
dp[a][b][c]=dp[a][b][c-1]+dp[a][b-1][c-1]-dp[a][b-1][c]
else:
dp[a][b][c]=dp[a-1][b][c]+dp[a-1][b-1][c]+dp[a-1][b][c-1]-dp[a-1][b-1][c-1]
print(w(a,b,c) = dp[a][b][c])
'백준 > DP' 카테고리의 다른 글
[dp/백준] 1915번: 가장 큰 정사각형 (0) | 2022.06.19 |
---|---|
[dp/백준] 11660번: 구간 합 구하기 5 (0) | 2022.06.19 |
[dp/백준] 9252번: LCS2 (0) | 2022.06.18 |
[dp/백준] 1937번: 욕심많은 판다(다시 풀어보기) (0) | 2022.06.18 |
[dp/백준] 1890번: 점프 (0) | 2022.06.17 |