boj9184-신나는 함수 실행

먼지감자·2021년 6월 12일
0

코딩테스트

목록 보기
18/37

문제

https://www.acmicpc.net/problem/9184

코드

import sys
input = sys.stdin.readline

dptable = [[[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 dptable[a][b][c]:
        return dptable[a][b][c]

    if (a<b and b<c):
        dptable[a][b][c] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
        return dptable[a][b][c]
    dptable[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 dptable[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)}')

풀이

memorization을 통해 배열에 값을 저장해놓고
값이 있으면 그 값 반환, 없으면 재쉬 호출

profile
ML/AI Engineer

0개의 댓글