Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17-wkdghdwns199 #68

Merged
merged 3 commits into from
Apr 29, 2024
Merged

17-wkdghdwns199 #68

merged 3 commits into from
Apr 29, 2024

Conversation

wkdghdwns199
Copy link
Collaborator

πŸ”— 문제 링크

https://www.acmicpc.net/problem/9184 - μ‹ λ‚˜λŠ” ν•¨μˆ˜ μ‹€ν–‰

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ - DP λŠ” μž¬κ·€λ₯Ό ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 고정관념을 버렀라! λˆ„μ κ°’μ„ λ§Œλ“€λ©΄ λœλ‹€!

✨ μˆ˜λ„ μ½”λ“œ

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)

이 μž¬κ·€ ν•¨μˆ˜λ₯Ό 동적 κ³„νšλ²• ν˜•νƒœλ‘œ λ°”κΎΈλŠ” λ¬Έμ œμ˜€λŠ”λ°, Bottom Up λ°©μ‹μœΌλ‘œ ν’€λ €λ‹€κ°€ μ‹œκ°„μ„ μž‘μ•„λ¨Ήμ—ˆλ„€μš”. λˆ„μ κ°’μ„ μž¬κ·€λ‘œ 받아도 λ˜λŠ”κ±΄λ° 고정관념에 μž‘ν˜€μžˆμ—ˆλ˜ 것 κ°™μŠ΅λ‹ˆλ‹€.

첫 λ²ˆμ§Έμ™€ 두 번째 if 문을 톡해 dp의 λ²”μœ„λ₯Ό μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
0보닀 μž‘μœΌλ©΄ 1둜 리턴해주고,
20보닀 크면 w(20,20,20)을 ν˜ΈμΆœν•˜λ©΄ 되기 λ•Œλ¬Έμ—
dp λŠ” 3차원 λͺ¨λ‘ λ‹€ 0~20 κΉŒμ§€λ§Œ μ΄ˆκΈ°ν™” μ‹œμΌœλ©΄ λ©λ‹ˆλ‹€. (a,b,c 의 값이 20을 λ„˜μ–΄κ°ˆ 일이 μ—†λ‹€λŠ” 말)
그리고 dp 값이 μ‘΄μž¬ν•˜λ©΄ λ°”λ‘œ 리턴, μ—†λ‹€λ©΄ κ΅¬ν•˜κ³  λ¦¬ν„΄ν•˜λ©΄ λ©λ‹ˆλ‹€!

μ™œ 이게 μ‹œκ°„μ΄ 였래 걸리지 μ•ŠλŠ”κ°€λ₯Ό 생각을 ν•΄λ³΄μ•˜λŠ”λ°, λͺ¨λ“  경우의 수λ₯Ό λ‹€ μ‚΄νŽ΄λ³΄μ§€ μ•Šκ³  λˆ„μ κ°’μ΄ 있으면 λ°”λ‘œ λ˜μ Έμ€„ 수 있기 λ•Œλ¬Έμ—
μ‹œκ°„μ΄ ν™•! μ€„μ–΄λ“œλŠ” 것이라고 λ³Ό 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€!

import sys
input = sys.stdin.readline

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]
    if 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]
    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]

dp = [[[0] * 21 for _ in range(21)] for _ in range(21)] 

while True:
    a,b,c = map(int, input().split())
    if a==b==c==-1 : break
    print(f'w({a}, {b}, {c}) = {w(a,b,c)}')

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

νŒŒμ΄μ¬μ€ 3차원 리슀트 μ‰½κ²Œ λ§Œλ“€ 수 μžˆμœΌλ‹ˆ λ„ˆλ¬΄ μ–΄λ ΅κ²Œ μƒκ°ν•˜μ§€ 말자!

@alstjr7437
Copy link
Member

image

μ €λŠ” 괜히 μž¬κ·€ ν•¨μˆ˜κ°€ μ–΄λ–€μ‹μœΌλ‘œ λŒμ•„κ°€λŠ”κ±΄μ§€ μ΄ν•΄ν•˜λ €λ‹€κ°€ κ·Έλƒ₯ DP둜 λˆ„μ κ°’μœΌλ‘œ ν•˜λ©΄ λ˜λŠ”κ±Έ 괜히 였기 λΆ™μ–΄μ„œ 많이 λ²„λ Έκ΅°μš”..

@wkdghdwns199
Copy link
Collaborator Author

wkdghdwns199 commented Mar 29, 2024

@alstjr7437 λΉ‘μ„Έλ”λΌκ΅¬μš”.. γ…‹γ…‹γ…‹ μž¬κ·€λ₯Ό μ•ˆν•΄λ„ λ˜λ˜κ°€μš”?

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3차원 dp λ„€μš” 문제 ν•¨μˆ˜ μ΄ν•΄ν•˜λŠ”κ²Œ λ„ˆλ¬΄ μ–΄λ €μ› μŠ΅λ‹ˆλ‹€ γ…‹γ…‹

@alstjr7437
Copy link
Member

@alstjr7437 λΉ‘μ„Έλ”λΌκ΅¬μš”.. γ…‹γ…‹γ…‹ μž¬κ·€λ₯Ό μ•ˆν•΄λ„ λ˜λ˜κ°€μš”?

μž¬κ·€λŠ” λ§žλŠ”λ° νŠΉμ •ν•œ 수λ₯Ό κ΅¬ν•˜λŠ” 식인가 ν•˜λ©΄μ„œ 깊게 파고 λ“€μ—ˆλŠ”λ° κ·Έλƒ₯ 문제λ₯Ό μœ„ν•΄μ„œ λ§Œλ“  μž¬κ·€μ˜€λ„€μš”..

@wkdghdwns199
Copy link
Collaborator Author

3차원 dp λ„€μš” 문제 ν•¨μˆ˜ μ΄ν•΄ν•˜λŠ”κ²Œ λ„ˆλ¬΄ μ–΄λ €μ› μŠ΅λ‹ˆλ‹€ γ…‹γ…‹

μ§„μ§œ λ‹€μ‹œ 봐도 이해 μ•ˆλ˜λŠ”κ²Œ λ ˆμ „λ“œμž…λ‹ˆλ‹€..

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리턴해쀄 λ•Œ DP둜만 λ°”κΏ”μ£Όλ©΄ λ˜λŠ”κ΅°μš”.! 문제 μ΄ν•΄ν•˜λŠ”λ° 은근 μ˜€λž˜κ±Έλ ΈμŠ΅λ‹ˆλ‹€..

import sys
input = sys.stdin.readline

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

    if dp[a][b][c] :
        return dp[a][b][c]

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



dp = [[[0 for _ in range(21)] for _ in range (21)] for _ in range (21)]
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)}")

@wkdghdwns199 wkdghdwns199 merged commit 5e169fc into main Apr 29, 2024
2 checks passed
@wkdghdwns199 wkdghdwns199 deleted the 17-wkdghdwns199 branch April 29, 2024 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants