[๋ฐฑ์ค€/python] 9184๋ฒˆ : ์‹ ๋‚˜๋Š” ํ•จ์ˆ˜ ์‹คํ–‰

2021. 3. 16. 23:46

๋ฌธ์ œ

์žฌ๊ท€ ํ˜ธ์ถœ๋งŒ ์ƒ๊ฐํ•˜๋ฉด ์‹ ์ด ๋‚œ๋‹ค! ์•„๋‹Œ๊ฐ€์š”?

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฌ๊ท€ํ•จ์ˆ˜ w(a, b, c)๊ฐ€ ์žˆ๋‹ค.

if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
    1

if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
    w(20, 20, 20)

if a < b and b < c, then w(a, b, c) returns:
    w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)

otherwise it returns:
    w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)

 

์œ„์˜ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์‰ฝ๋‹ค. ํ•˜์ง€๋งŒ, ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๊ฐ’์„ ๊ตฌํ•˜๋Š”๋ฐ ๋งค์šฐ ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. (์˜ˆ๋ฅผ ๋“ค๋ฉด, a=15, b=15, c=15)

a, b, c๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, w(a, b, c)๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ž…๋ ฅ์€ ์„ธ ์ •์ˆ˜ a, b, c๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰์€ -1 -1 -1๋กœ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์„ธ ์ •์ˆ˜๊ฐ€ ๋ชจ๋‘ -1์ธ ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰์„ ์ œ์™ธํ•˜๋ฉด ์—†๋‹ค.

 

์ถœ๋ ฅ

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๊ฐ๊ฐ์˜ a, b, c์— ๋Œ€ํ•ด์„œ, w(a, b, c)๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ œํ•œ

  • -50 ≤ a, b, c ≤ 50

 

์ •๋‹ต

answer = []
dp = {}


def w(a, b, c):
    abc = str(a) + "/" + str(b) + "/" + str(c)
    if abc in dp:
        return dp[abc]

    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 a < b and b < c:
        dp[abc] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
        return dp[abc]
    else:
        dp[abc] = 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[abc]


while 1:
    a, b, c = list(map(int, input().split()))
    if a == -1 and b == -1 and c == -1:
        break
    else:
        a = 'w({0}, {1}, {2}) = {3}'.format(a, b, c, w(a, b, c))
        answer.append(a)

for a in answer:
    print(a)

์œ„ ๋ฌธ์ œ๋Š” ๋™์ ๊ณ„ํš๋ฒ•(Dynamic Programming)์„ ์ž˜ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

dp๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํ•œ๋ฒˆ ๊ณ„์‚ฐํ•œ ๊ฐ’์€ dp์— ์ €์žฅํ•œ๋‹ค.

๋ฌธ์ œ์—์„œ์ฒ˜๋Ÿผ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ™์€ ๊ณ„์‚ฐ์ด๋ผ๋„ ํ•  ๋•Œ๋งˆ๋‹ค ๊ณ„์‚ฐํ•˜์ง€๋งŒ

๋™์ ๊ณ„ํš๋ฒ•์„ ์ด์šฉํ•ด dp์— ์ €์žฅํ•˜๋ฉด ์ „์— ๋‚˜์˜จ์  ์žˆ๋Š” ๊ณ„์‚ฐ์€ dp์— ์ €์žฅ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

 

def w(a, b, c):
    abc = str(a) + "/" + str(b) + "/" + str(c)
    if abc in dp:
        return dp[abc]

    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 a < b and b < c:
        dp[abc] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
        return dp[abc]
    else:
        dp[abc] = 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[abc]

ํ•จ์ˆ˜ w(a,b,c)๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

dp ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๊ฐ€ ๋  abc๋ฅผ "a๊ฐ’/b๊ฐ’/c๊ฐ’"์ธ ๋ฌธ์ž์—ด๋กœ ์ €์žฅํ•œ๋‹ค.

๋งŒ์•ฝ abc๊ฐ€ dp์— ์ด๋ฏธ ํ‚ค๋กœ ์กด์žฌํ•œ๋‹ค๋ฉด ์ด์ „์— ๊ณ„์‚ฐํ•ด์„œ ๊ฐ’์„ ์ €์žฅํ•ด๋†จ๋‹จ ์˜๋ฏธ์ด๋ฏ€๋กœ ๊ฐ’์„ returnํ•œ๋‹ค. 

 

๊ทธ ์•„๋ž˜์˜ 3๊ฐœ์˜ if๋ฌธ๊ณผ else๋ฌธ์€ ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์กฐ๊ฑด ๊ทธ๋Œ€๋กœ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

๋Œ€์‹  ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ๋•Œ dp์— ์ €์žฅํ•ด๊ฐ€๋ฉฐ ์‹คํ–‰ํ•ด์•ผ ๊ฐ™์€ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•˜๋Š” ์ผ์ด ์—†๋‹ค.

 

๋™์ ๊ณ„ํš๋ฒ•(Dynamic Programming)์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ ์•„๋ž˜์˜ ํฌ์ŠคํŒ…์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

2021.03.25 - [๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜/๐Ÿ”…๊ฐœ๋…์ •๋ฆฌ] - [ํŒŒ์ด์ฌ/python] ๋™์ ๊ณ„ํš๋ฒ• : Dynamic Programming

 

[ํŒŒ์ด์ฌ/python] ๋™์ ๊ณ„ํš๋ฒ• : Dynamic Programming

๋™์ ๊ณ„ํš๋ฒ•, Dynamic Programming์€ ์•„๋ž˜ ๋‘ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์ตœ์  ๋ถ€๋ถ„ ๊ตฌ์กฐ : ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ž‘์€ ๋ฌธ์ œ์˜ ๋‹ต์„ ๋ชจ์•„ ํฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ์ค‘๋ณต ๋ถ€๋ถ„ ๊ตฌ์กฐ : ๋™์ผํ•œ ์ž‘์€

ye333.tistory.com

 

728x90

BELATED ARTICLES

more