๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/์•Œ๊ณ ๋ฆฌ์ฆ˜-Python

[์•Œ๊ณ ๋ฆฌ์ฆ˜/Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํƒ€๊ฒŸ ๋„˜๋ฒ„ (DFS)

by Danna 2021. 3. 6.
728x90
728x90

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ ์„ค๋ช…

ํ’€์ด1

  • DFS ๋ฅผ ์–ด๋–ป๊ฒŒ ์ด์šฉํ• ์ง€ ๊ฐ์ด ์•ˆ์™€์„œ ๋น„ํšจ์œจ์ ์œผ๋กœ ํ’€์–ด๋ฒ„๋ ธ๋‹ค.
  • ๊ฐ€๋Šฅํ•œ ์ผ€์ด์Šค๋ฅผ ์ „๋ถ€ ๊ณ ๋ คํ•ด์„œ ๊ตฌํ˜„
  • itertools.product ์ด์šฉํ•ด์„œ ๊ณฑ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

solution ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋ฆฌํ„ด๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • numbers : ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด
  • target : ํƒ€๊ฒŸ ๋„˜๋ฒ„
  • return : ์ˆซ์ž๋ฅผ ์ ์ ˆํžˆ ๋”ํ•˜๊ณ  ๋นผ์„œ ํƒ€๊ฒŸ ๋„˜๋ฒ„๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜

์ž์„ธํ•œ ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • product ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฐ€๋Šฅํ•œ case ๊ตฌํ•˜๊ธฐ (cases ์˜ˆ์‹œ๋Š” ๊ทธ๋ฆผ ์ฐธ๊ณ )
  • ๋ง์…ˆ๊ณผ ๋บ„์…ˆ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ [-1, 1] ์„ ๊ณฑํ•˜๋Š” ๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. 
  • length ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ case[i] * numbers[i] ๋ฅผ ๋”ํ•œ ๊ฐ’์ด target ๊ณผ ๊ฐ™์œผ๋ฉด count ๋ฅผ ์ฆ๊ฐ€์‹œ์ผฐ๋‹ค.
from itertools import product

def solution(numbers, target):
    length = len(numbers)
    # ์ „์ฒด ๊ฐ€๋Šฅํ•œ case ๊ตฌํ•˜๊ธฐ
    cases = []
    for _ in range(length):
        cases.append([-1, 1])
    cases = list(product(*cases))

    # target ์ธ ๊ฒฝ์šฐ๋งŒ count ํ•˜๊ธฐ
    count = 0
    for case in cases:
        s = 0
        for i in range(length):
            s += case[i] * numbers[i]
        if s == target:
            count += 1
    return count

print(solution([1, 1, 1, 1, 1], 3))

 

ํ’€์ด2

  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ DFS ๋ฅผ ์ด์šฉํ•œ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.
  • ํ˜„์žฌ ๊ฐ’์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ๋Š” ๊ณผ์ •์—์„œ DFS ๋ฅผ ๊ฐ๊ฐ ํ˜ธ์ถœํ•œ๋‹ค.
    • ๋”ํ•œ ๊ฐ’์„ ํ†ตํ•ด DFS ์žฌ๊ท€ํ˜ธ์ถœ
    • ๋บ€ ๊ฐ’์„ ํ†ตํ•ด DFS ์žฌ๊ท€ ํ˜ธ์ถœ
  • index ๋ฅผ ๊ณ„์‚ฐํ•ด ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๋”ํ–ˆ์„ ๋•Œ ์ข…๋ฃŒ๋˜๋„๋ก ํ•œ๋‹ค.
  • ์ข…๋ฃŒ๋˜๊ธฐ ์ „์— ๋”ํ•œ ๊ฐ’์ด target ๊ณผ ๊ฐ™์œผ๋ฉด answer ์ฆ๊ฐ€
answer = 0
def dfs(idx, numbers, target, value):
    global answer # ์ „์—ญ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์„ ์–ธ

    length = len(numbers)
    # ์ „๋ถ€ ๋‹ค ๊ณ„์‚ฐํ–ˆ์„ ๋•Œ target ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ, ์ •๋‹ต
    if(idx == length and target == value):
        answer += 1
        return
    # ์ „๋ถ€ ๋‹ค ๊ณ„์‚ฐํ–ˆ์„ ๋•Œ target ๊ณผ ๋‹ค๋ฅธ ๊ฒฝ์šฐ,
    if(idx == length):
        return 

    # ํ˜„์žฌ ๊ฐ’์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ์„œ value ๋กœ ์„ค์ •, ๋‹ค์Œ index ๊ณ„์‚ฐ์„ ์œ„ํ•ด dfs ๋ฐ˜๋ณต
    dfs(idx+1, numbers, target, value+numbers[idx])
    dfs(idx+1, numbers, target, value-numbers[idx])


def solution(numbers, target):
    global answer # ์ „์—ญ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์„ ์–ธ
    dfs(0, numbers, target, 0)
    return answer

print(solution([1, 1, 1, 1, 1], 3))

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋งํฌ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํƒ€๊ฒŸ ๋„˜๋ฒ„

n๊ฐœ์˜ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ˆ˜๋ฅผ ์ ์ ˆํžˆ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ์„œ ํƒ€๊ฒŸ ๋„˜๋ฒ„๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [1, 1, 1, 1, 1]๋กœ ์ˆซ์ž 3์„ ๋งŒ๋“ค๋ ค๋ฉด ๋‹ค์Œ ๋‹ค์„ฏ ๋ฐฉ๋ฒ•์„ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -1+1+1+1+1 = 3 +1-1+1+1+

programmers.co.kr

 

์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ

  • ๋น„ํšจ์œจ์ ์ผ์ง€๋ผ๋„ ์ฒซ๋ฒˆ์งธ ํ’€์ด๋Š” ์Šค์Šค๋กœ ํ’€์–ด๋ณด๋„๋ก ํ•œ๋‹ค.
  • ์ •๋‹ต์ด๋”๋ผ๋„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ๊ณ ์ณ๋‚˜๊ฐ€์ž.
  • DFS / BFS ๊ด€๋ จ ๋ฌธ์ œ๋ฅผ ๋” ํ’€์–ด๋ณด์ž.

 

728x90
728x90