[백준 BOJ_9184] 신나는 함수 실행 Python 풀이
출처: 백준 온라인 저지
문제
풀이
코드
def w(a, b, c):
if a <= 0 or b <= 0 or c <= 0:
return 1
if cache[a][b][c] == -51:
if a > 20 or b > 20 or c > 20:
cache[a][b][c] = w(20, 20, 20)
elif a < b and b < c:
cache[a][b][c] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
else:
cache[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 cache[a][b][c]
# cache를 곱셈으로 초기화할경우 나중에 값이 복사될 수 있다.
cache = [[[-51 for _ in range(51)] for _ in range(51)] for _ in range(51)]
cache[0][0][0] = 1
while(1):
A, B, C = map(int, input().split())
if A == -1 and B == -1 and C == -1:
break
print("w({}, {}, {}) = {}".format(A, B, C, w(A, B, C)))
Leave a comment