๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 9184 - ์‹ ๋‚˜๋Š” ํ•จ์ˆ˜ ์‹คํ–‰

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 9184๋ฒˆ

ํ’€์ด ๊ณผ์ •

๋ฉ”๋ชจ์ด์ œ์ด์…˜ ์œผ๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋ฌธ์ œ์—์„œ ์ œ์‹œ๋œ ํ•จ์ˆ˜ ๊ตฌํ˜„์„ ๋™์  ๊ณ„ํš๋ฒ• ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys

memo = [[[-1] * 51 for _ in range(51)] for _ in range(51)]


def func(a, b, c):
    if a <= 0 or b <= 0 or c <= 0:
        return 1
    if a > 20 or b > 20 or c > 20:
        return func(20, 20, 20)

    if memo[a][b][c] != -1:
        return memo[a][b][c]

    if a < b < c:
        memo[a][b][c] = func(a, b, c - 1) + func(a, b - 1, c - 1) - func(a, b - 1, c)
    else:
        memo[a][b][c] = func(a - 1, b, c) + func(a - 1, b - 1, c) + func(a - 1, b, c - 1) - func(a - 1, b - 1, c - 1)

    return memo[a][b][c]


def solution(a, b, c):
    output = func(a, b, c)

    return f"w({a}, {b}, {c}) = {output}\n"


if __name__ == '__main__':
    while 1:
        a, b, c = list(map(int, sys.stdin.readline().split()))

        if [a, b, c] == [-1, -1, -1]:
            break

        answer = solution(a, b, c)
        sys.stdout.write(answer)
๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€