[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 15649 - N๊ณผ M (1)

keyneneยท2022๋…„ 10์›” 25์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
14/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 15649 - N๊ณผ M (1)

๐Ÿ“œ๋ฌธ์ œ




๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

Back-Tracking(๋ฐฑํŠธ๋ž˜ํ‚น)

  • ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ค‘์—์„œ ํŠน์ •ํ•œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๋งŒ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ์ฆ‰, ํ•ด๊ฐ€ ๋  ๋งŒํ•œ์ง€ ์‚ฌ์ „์— ํŒ๋‹จํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋˜๋Œ์•„๊ฐ€์—ฌ(Back-Tracking) ํƒ์ƒ‰ํ•จ

n,m์„ ์ž…๋ ฅ๋ฐ›๊ณ  num์ด๋ผ๋Š” list๋ฅผ ์ •์˜ํ•˜์ž
์ž๋ฆฟ์ˆ˜๋งˆ๋‹ค n์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งŒํผ ์ˆœ์„œ๋Œ€๋กœ ๊ณ ๋ฅด๋Š” ํ•จ์ˆ˜ bt()๋ฅผ ๋งŒ๋“ค์ž
for loof์œผ๋กœ i๋ฅผ 1~n๋งŒํผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ num์— append()ํ•˜์—ฌ num ๊ธธ์ด๊ฐ€ m์ด๋ฉด ์ถœ๋ ฅ,
m๋ณด๋‹ค ์ž‘์œผ๋ฉด bt()๋ฅผ ์žฌํ˜ธ์ถœํ•˜์ž

โ€ป m์ž๋ฆฟ์ˆ˜๋งŒํผ append() ํ•ด์•ผํ•˜๋ฏ€๋กœ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ–ˆ์Œ


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. n, m, num(list), bt(funtion)์ •์˜ ํ›„ ์‹คํ–‰ํ•˜์ž
#bt()๋Š” ์žฌ๊ท€ํ•จ์ˆ˜๋กœ, ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๊ณ„ํ•˜์ž

def bt():
  if num๊ธธ์ด๊ฐ€ m์ผ๋•Œ:
    ๊ฒฐ๊ณผ์ถœ๋ ฅ
  for i๋ฅผ 1~n๊นŒ์ง€:
    ์ž๋ฆฟ์ˆ˜๋งŒํผ bt()ํ˜ธ์ถœํ•˜๋ฉฐ ์ˆซ์ž๋ฝ‘๊ธฐ
bt()

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline

n,m = map(int, input().split())
num = []

def bt():
    if len(num) == m:
        print(*num)
        return

    for i in range(1,n+1):
        if i not in num:
            num.append(i)
            bt()
            num.pop()
bt()

โœ๏ธ1. n๊ฐ€์ง€ ์ˆซ์ž๋ฅผ m์ž๋ฆฟ์ˆ˜๋งŒํผ ์ค‘๋ณต์—†์ด ์ถœ๋ ฅํ•˜๊ธฐ (์ „์ฒด์ฝ”๋“œ๋ถ„์„)

import sys
input = sys.stdin.readline

n,m = map(int, input().split())
num = []

def bt():
	#num์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•˜์—ฌ m์ด๋ฉด ์ถœ๋ ฅ ํ›„ return
    if len(num) == m:
        print(*num)
        return

	#1~n๊นŒ์ง€ ์ˆซ์ž ๋ฝ‘๊ธฐ
    for i in range(1,n+1):
    	#i๊ฐ€ ํ˜„์žฌ num์— ์—†์„ ๋•Œ๋งŒ ์‹คํ–‰ (์ค‘๋ณต์ œ๊ฑฐ)
        if i not in num:
            num.append(i)
            
            #๋‹ค์Œ์ž๋ฆฟ์ˆ˜ ๋ฝ‘๊ธฐ์œ„ํ•ด bt()์žฌํ˜ธ์ถœ (์žฌ๊ท€)
            bt()
            
            #bt() ์ƒ๋‹จ์˜ num ๊ธธ์ด ํ™•์ธํ•˜๋Š” if์—์„œ return ๋˜๋ฉด,
            #์ œ์ผ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ
            num.pop()
bt()

โ“์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๊ฒŒ ๋ ๊นŒ

โ€ปn,m = 4,2์ธ ๊ฒฝ์šฐ
1. bt()์˜ if๋Š” ๋„˜์–ด๊ฐ€๊ณ  for loof์—์„œ i์— [1]์ด ๋“ค์–ด์˜จ๋‹ค.
2. num์— 1์ด append๋œ๋‹ค. (num = [1])
3. for loof์—์„œ bt()๊ฐ€ ์žฌํ˜ธ์ถœ ๋œ๋‹ค.
4. 2๋ฒˆ์งธ ํ˜ธ์ถœ๋œ bt()์˜ for loof์˜ i๋Š” 1๋ถ€ํ„ฐ ๋‹ค์‹œ ๋™์ž‘ํ•˜๋Š”๋ฐ, 
   "if i not in num"์— ์ถฉ์กฑ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ (1์ด ์ค‘๋ณต) i=2๊ฐ€ ๋œ๋‹ค.
5. num์— 2๊ฐ€ append๋œ๋‹ค. (num = [1,2])
6. for loof์—์„œ bt()๊ฐ€ ๋˜ ์žฌํ˜ธ์ถœ ๋œ๋‹ค. (3๋ฒˆ์งธ)
7. 3๋ฒˆ์งธ bt()์—์„œ "if len(num)==m"์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์—ฌ num์„ ์ถœ๋ ฅํ•˜๊ณ  return
8. return ํ›„ num.pop()์„ ๋งŒ๋‚˜, num์˜ ์ œ์ผ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. (num = [1])
9. 4๋ฒˆ์—์„œ ๋‹ค์‹œ ๋„๋Š” for loof์˜ i๋Š” 3์ด ๋˜์–ด, ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

๐Ÿ‘‰๐Ÿป๋™์ž‘๊ณผ์ •
[1] โ†’ [1,2]์ถœ๋ ฅ โ†’ [1] โ†’ [1,3]์ถœ๋ ฅ โ†’ [1] โ†’ [1,4]์ถœ๋ ฅ ...

๐Ÿ“š์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ค๋ฅ˜์™€ ์ •๋ฆฌ

โ€ป์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

import sys
input = sys.stdin.readline

n,m = map(int, input().split())
num = []

def bt():
    if len(num) == m:
        print(*num)
        del num[-1]

    for i in range(1,n+1):
        if i not in num:
            num.append(i)
            bt()
bt()
โ“๋‚ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ‹€๋ฆฐ ์ด์œ 

num์— ์ดˆ๊ธฐ๊ฐ’ [1,2]๋ฅผ ์ €์žฅํ•˜๊ณ , bt()๊ฐ€ ์žฌํ˜ธ์ถœ๋˜์–ด if๋ฌธ ๋งŒ์กฑ ํ›„ ์ถœ๋ ฅํ–ˆ๋Š”๋ฐ,
์žฌ๊ท€ํ•จ์ˆ˜ bt()๊ฐ€ ์ข…๋ฃŒ๋˜์ง€ ์•Š์•„์„œ for loof์˜ i๊ฐ€ ๋‹ค์‹œ 1๋ถ€ํ„ฐ ๋Œ๊ธฐ ๋•Œ๋ฌธ์—,
[1] โ†’ [1,2] โ†’ [1] โ†’ for loof i=1์ด๋ผ ๋‹ค์‹œ 2๋ถ€ํ„ฐ ์ €์žฅ [1,2]๊ฐ€ ๋ฐ˜๋ณต๋œ๋‹ค.

โ—์žฌ๊ท€ํ•จ์ˆ˜ ์‚ฌ์šฉ ์‹œ for loof์˜ ๊ฐ’์„ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ์žฌ๊ท€๋œ ํ•จ์ˆ˜์˜ ์ข…๋ฃŒ(return)์€ ํ•„์ˆ˜!
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€