๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

โšก๏ธ baekjoon. 14501 ํ‡ด์‚ฌ [Silver III][python]

ttoance 2023. 6. 28. 11:19

 

๊ด€๋ จ ํ’€์ด๋ฅผ ๋ดค์„๋•Œ ํƒ์š•๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋‚˜ ํ•˜๋‹ค๊ฐ€, ์ฃผ์–ด์ง„ ์˜ˆ์‹œ ์ผ€์ด์Šค๋กœ ๋„์ €ํžˆ ์ ‘๊ทผํ•  ์ˆ˜๊ฐ€ ์—†์–ด์„œ,

dfs๋กœ ์ ‘๊ทผํ•ด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•ด ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€ ์ตœ๋Œ“๊ฐ’์„ ๋ณด์—ฌ์ฃผ๋Š” ์ ‘๊ทผ ๋ฐฉ์‹์„ ํƒํ–ˆ๋‹ค. 

N = int(input())

results = list()

# history ๋ณ€์ˆ˜๋Š” debug ์šฉ๋„ 
def dfs(start, current, history):
    if (start >= len(meetingList)):
        results.append(current)
        return
    
    # ์˜ค๋Š˜ ์žกํ˜€์žˆ๋Š” ๋ฏธํŒ…์„ ์ง„ํ–‰ํ•  ๊ฒฝ์šฐ 
    next = start + meetingList[start][0]
    if (next <= len(meetingList)):
        dfs(next, current + meetingList[start][1], history + str(start))
    
    # ์˜ค๋Š˜ ์žกํ˜€์žˆ๋Š” ๋ฏธํŒ…์„ ์ง„ํ–‰ํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ 
    for i in range(start + 1, next):
        dfs(i, current, history)
    

meetingList = list()
for _ in range(N):
    meetingList.append(list(map(int, input().split())))

dfs(0, 0, '')
results.sort()
print(results[-1])

 

+) ๋‹ค๋ฅธ ํ’€์ด๋กœ๋Š” ๋™์  dp๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. 

https://zu-techlog.tistory.com/42

 

[BOJ] ๋ฐฑ์ค€ 14501๋ฒˆ ํ‡ด์‚ฌ - ํŒŒ์ด์ฌ(Python)

๋ฌธ์ œ ์ƒ๋‹ด์›์œผ๋กœ ์ผํ•˜๊ณ  ์žˆ๋Š” ๋ฐฑ์ค€์ด๋Š” ํ‡ด์‚ฌ๋ฅผ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์˜ค๋Š˜๋ถ€ํ„ฐ N+1์ผ์งธ ๋˜๋Š” ๋‚  ํ‡ด์‚ฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ๋‚จ์€ N์ผ ๋™์•ˆ ์ตœ๋Œ€ํ•œ ๋งŽ์€ ์ƒ๋‹ด์„ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋ฐฑ์ค€์ด๋Š” ๋น„์„œ์—๊ฒŒ ์ตœ๋Œ€ํ•œ ๋งŽ์€ ์ƒ๋‹ด

zu-techlog.tistory.com

 

๋ฐ˜์‘ํ˜•