๐Ÿง ๋ฐฑํŠธ๋ž˜ํ‚น ( Backtracking )

๐Ÿค” ๋ฐฑํŠธ๋ž˜ํ‚น์ด๋ž€?

DFS์˜ ์ผ์ข…์ธ ๋ฐฑํŠธ๋ž˜ํ‚น์€ ์ผ๋ฐ˜์ ์ธ DFS์™€ ๋‹ฌ๋ฆฌ ๊ฐ€์ง€์น˜๊ธฐ(pruning)๋ฅผ ์ง„ํ–‰
๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋Œ€์‹  ์กฐ๊ฑด์„ ๊ฑฐ๋Š”๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
- ์œ ๋ง(promising)ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ํƒ์ƒ‰์„ ์ค‘์ง€ํ•˜๊ณ  ์ด์ „ ๋…ธ๋“œ๋กœ ๋Œ์•„๊ฐ€์„œ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
๋ฐฑํŠธ๋ž˜ํ‚น์€ DFS, BFS ๋‘๊ฐ€์ง€๋กœ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ DFS๋กœ ๊ตฌํ˜„ํ•˜๋Š”๊ฒƒ์ด ๋” ํŽธํ•˜๋‹ค.
(BFS๋Š” ํ์˜ ํฌ๊ธฐ๊ฐ€ ์ปค์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณดํ†ต ์ผ๋ฐ˜์ ์œผ๋กœ DFS๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค)
๋‹ค๋งŒ ํŠธ๋ฆฌ์˜ ๊นŠ์ด๊ฐ€ ๋ฌดํ•œ๋Œ€๊ฐ€ ๋  ๋•Œ, BFS๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
๋˜ํ•œ ์ผ๋ฐ˜์ ์œผ๋กœ DFS๋Š” ์Šคํƒ์ด๋‚˜ ์žฌ๊ท€๋กœ, BFS๋Š” ํ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

๋ฐฑํŠธ๋ž˜ํ‚น์€ ๋‹ต์ด ๋  ์ˆ˜ ์—†๋Š” ํ›„๋ณด๋Š” ๋”์ด์ƒ ๊นŠ๊ฒŒ ๋“ค์–ด๊ฐ€์ง€ ์•Š๊ณ  ๋˜๋Œ์•„๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
 

 

 

๐Ÿค” ๋ฐฑํŠธ๋ž˜ํ‚น ์ˆ˜๋„์ฝ”๋“œ

procedure backtracking(c) is
    if reject(P, c) then return
    if accept(P, c) then output(P, c)
    s ← first(P, c)
    while s ≠ NULL do
        backtracking(s)
        s ← next(P, s)
reject(P,c): ๋งŒ์•ฝ ๋‹ค์Œ์˜ ๋…ธ๋“œ๊ฐ€ ํ›„๋ณด๊ฐ€ ์•„๋‹Œ๋‹ค๋ฉด return์œผ๋กœ ์ข…๋ฃŒ.
accept(P,c): ๋งŒ์•ฝ ํ˜„์žฌ ๋…ธ๋“œ๊นŒ์ง€๊ฐ€ ๋‹ต๊ณผ ์ผ์น˜ํ•œ๋‹ค๋ฉด ๋‹ต์„ ์ถœ๋ ฅ.
first(P,c): ํ›„๋ณด C์˜ ์ฒซ๋ฒˆ์งธ ํ™•์žฅ์„ ์‹œ์ž‘ํ•œ๋‹ค.
๋‹ต์ด ์—†์„๋•Œ๊นŒ์ง€ ๋‹ค์Œ ํ•จ์ˆ˜๋ฅผ ๊ณ„์† ๋ฐ˜๋ณต.

 

 

 

๐Ÿง ๋ฐฑ์ค€ 15649


๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ .  ๋ฐฑํŠธ๋ž˜ํ‚น

DFS์˜ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•œ๋‹ค.
์ด๋•Œ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•ต์‹ฌ์€ ์Šคํƒ์„ ์ด์šฉํ•œ ๊ตฌํ˜„์—์„œ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ๋ฐฉ์‹์ด ์Šคํƒ์˜ ๋™์ž‘๋ฐฉ์‹์ž„์„ ์ด์šฉํ•œ๋‹ค.
ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ = push,  ํ•จ์ˆ˜์˜ ์ข…๋ฃŒ = pop๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.



๐Ÿค”
 Algorithm ๊ณผ์ • 
1. ํ•ด๋‹นํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์Šคํƒ์— pushํ•˜๊ณ  ๋™์ž‘์ด ๋๋‚˜๋ฉด ๋‹ค์‹œ ๋นผ๋‚ด๋Š” pop์„ ์ง„ํ–‰
2. ์ด๋ ‡๊ฒŒ ์ง„ํ–‰ํ•  ๋•Œ, ์ด๋ฏธ ์„ ํƒํ•œ ์ˆซ์ž๋Š” ๋ฐฐ์ œ (1 1์€ ์•ˆ๋˜๋ฏ€๋กœ)ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊ฐ€์ง€์น˜๊ธฐ(pruning)

 

๐Ÿคซ  solution_15649 (Backtracking)

n, m = map(int, input().split())

stack = []
def f():
    if len(stack) == m:
        print(" ".join(map(str, stack)))
        return


    for i in range(1, n+1):
        if i in stack:
            continue
        stack.append(i)
        f()
        stack.pop()

f()



๐Ÿคซ  solution_15649 (itertools์˜  permutations ์‚ฌ์šฉ)

from itertools import permutations
n, m = map(int, input().split())
num = [i for i in range(1, n+1)]

ans = permutations(num, m)

for i in ans:
    for j in i:
        print(j, end=' ')
    print()

 

 

 

๐Ÿง ๋ฐฑ์ค€ 15650~15652


๐Ÿคซ  solution_15650

n, m = map(int, input().split())

stack = []
first = 1
def f(first):
    if len(stack) == m:
        print(" ".join(map(str, stack)))
        return


    for i in range(first, n+1):
        if i in stack:
            pass

        stack.append(i)
        f(i+1)
        stack.pop()
f(first)





๐Ÿคซ  solution_15651

n, m = map(int, input().split())

stack = []
def f():
    if len(stack) == m:
        print(" ".join(map(str, stack)))
        return


    for i in range(1, n+1):
        stack.append(i)
        f()
        stack.pop()
f()




๐Ÿคซ  solution_15652

n, m = map(int, input().split())

stack = []

first = 1
def f(first):
    if len(stack) == m:
        print(" ".join(map(str, stack)))
        return

    for i in range(first, n+1):
        stack.append(i)
        f(i)
        stack.pop()
f(first)โ€‹

 

 

 

 

+ Recent posts