๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ๋‹ค๋ฆฌ ๋†“๊ธฐ BOJ #1010

by seolhee2750 2022. 5. 26.
๋ฌธ์ œ

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

 

1010๋ฒˆ: ๋‹ค๋ฆฌ ๋†“๊ธฐ

์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์— ๋Œ€ํ•ด ๊ฐ•์˜ ์„œ์ชฝ๊ณผ ๋™์ชฝ์— ์žˆ๋Š” ์‚ฌ์ดํŠธ์˜ ๊ฐœ์ˆ˜ ์ •์ˆ˜ N, M (0 < N ≤ M < 30)์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import sys

t = int(sys.stdin.readline().strip())
dp = [[0 for _ in range(31)] for _ in range(31)]
for i in range(1, 31):
    for j in range(1, 31):
        if i == 1:
            dp[i][j] = j
        elif i == j:
            dp[i][j] = 1
        elif i < j:
            dp[i][j] = dp[i-1][j-1] + dp[i][j-1]

for _ in range(t):
    n, m = map(int, sys.stdin.readline().strip().split())
    print(dp[n][m])

๐Ÿ‘‰ DP/์ˆ˜ํ•™/์กฐํ•ฉ ๋ฌธ์ œ์ด๊ณ , ๋‚˜๋Š” DP๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

๋‹ค๋ฆฌ๋ฅผ ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์œ„์˜ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ–ˆ๊ณ ,

๊ทธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์„ ๊ฐ–๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

  1. n์ด 1์ผ ๊ฒฝ์šฐ์—๋Š” m
  2. n์ด m๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” 1
  3. n์ด m๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” dp[n-1][m-1] + dp[n][m-1]

 

+ ๋‚˜๋Š” ์ด๋ ‡๊ฒŒ dp๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์ดํ–ˆ์ง€๋งŒ,

์กฐํ•ฉ์„ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค,,!

< ๋‹ค๋ฅธ ํ’€์ด ์ฐธ๊ณ  ์‚ฌ์ดํŠธ >

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ

๋‚œ์ด๋„๊ฐ€ ๋‚ฎ์€ ๋ฌธ์ œ๋กœ ๋‚˜์™€์žˆ์ง€๋งŒ,,

์กฐํ•ฉ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์ดํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ์ ‘ํ•ด๋ณด๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์ด๋ ‡๊ฒŒ ๊ธ€์„ ๋‚จ๊ฒจ๋†“๋Š”๋‹ค.

์กฐํ•ฉ๋ก ๋„ ๊ณต๋ถ€ํ•ด๋‘๋ฉด ๋ฌธ์ œ ํ’€์ดํ•  ๋•Œ ํŽธํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€