백준/DP
[dp/백준] 9184번: 신나는 함수 실행
ydin
2022. 6. 19. 00:21
-문제: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])