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

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

Python

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

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

๐Ÿ“œ๋ฌธ์ œ




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

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

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

๋‚ด ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 15649 - N๊ณผ M (1)
์ด์ „ ํฌ์ŠคํŒ…๊ณผ ๊ฐ™์€๋ฐ, ์ค‘๋ณต์ด ๊ฐ€๋Šฅํ•˜๊ณ  ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ์ด๋ž€ ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

์ „๊ณผ ๋˜‘๊ฐ™์ด n,m๊ณผ num์ด๋ผ๋Š” list์™€ bt() ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜์ž
bt()๋Š” ๋˜ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ• ๊ป€๋ฐ, ์ด์ „๊ณผ ์กฐ๊ฑด์„ ๋‹ค๋ฅด๊ฒŒ ์ œ์‹œํ•˜๋ฉด ๋  ๊ฒƒ๊ฐ™๋‹ค.
bt์— ๋งค๊ฐœ๋ณ€์ˆ˜ start๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ start~n๊นŒ์ง€ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ(๋น„๋‚ด๋ฆผ์ฐจ์ˆœ),
num์— append()ํ•˜์—ฌ num ๊ธธ์ด๊ฐ€ m์ด๋ฉด ์ถœ๋ ฅ, m๋ณด๋‹ค ์ž‘์œผ๋ฉด bt(i)๋ฅผ ์žฌํ˜ธ์ถœํ•˜์ž

โ“bt(start)๋ฅผ i๋กœ ํ˜ธ์ถœํ•˜๋Š” ์ด์œ 

for loof์—์„œ i๊ฐ€ start~n๋งŒํผ ์ฆ๊ฐ€ํ• ํ…๋ฐ, num[1]๊ฐ’์ด num[0]๊ฐ’๊ณผ ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋ ค๋ฉด(๋น„๋‚ด๋ฆผ์ฐจ์ˆœ)
start๋ฅผ for loof์˜ i๋กœ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค.

โ€ป1์ผ๋•Œ๋Š” 1์„ ๋„˜๊ธฐ๊ณ , 2์ผ๋•Œ๋Š” 2๋ฅผ ๋„˜๊ธฐ๋ฉด์„œ "2,1"๊ณผ ๊ฐ™์€ ๋‚ด๋ฆผ์ฐจ์ˆœ์˜ ์ƒํ™ฉ์„ ์•„์˜ˆ ๊ฑธ๋Ÿฌ๋ฒ„๋ฆฌ๊ธฐ
  (Back-Tracking)

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

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

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

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

import sys
input = sys.stdin.readline

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

def bt(start):
    if len(num) == m:
        print(*num)
        return
    for i in range(start,n+1):
        num.append(i)
        bt(i)
        num.pop()
bt(1)

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

import sys
input = sys.stdin.readline

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

#bt()ํ•จ์ˆ˜์˜ ์‹œ์ž‘์  ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ธฐ
def bt(start):
    if len(num) == m:
        print(*num)
        return
        
    #for loof๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜ start๋ถ€ํ„ฐ n๊นŒ์ง€๋งŒ ๊ฒ€์‚ฌ (๋น„๋‚ด๋ฆผ์ฐจ์ˆœ)
    for i in range(start,n+1):
        num.append(i) #num๊ฐ’์— i๋ฅผ appendํ•˜๊ณ ๋‚˜์„œ
        bt(i)         #bt(i)๋ฅผ ์žฌํ˜ธ์ถœํ•œ๋‹ค (Back-Tracking)
        num.pop()
bt(1)

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

์ž์„ธํ•œ ๋™์ž‘์ˆœ์„œ๋Š” ๋‚ด ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 15649 - N๊ณผ M (1) ์ฐธ๊ณ 

โ€ปn,m = 4,2์ธ ๊ฒฝ์šฐ
์ „์ฒด ๋™์ž‘์ˆœ์„œ๋Š” ์œ„ ๋งํฌ์™€ ๋น„์Šทํ•œ๋ฐ bt์— ๋งค๊ฐœ๋ณ€์ˆ˜ start๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐ์„œ ๋‹ค๋ฅด๋‹ค.

๐Ÿ‘‰๐Ÿป๋™์ž‘๊ณผ์ •
[1] bt(1) โ†’ [1,1] return, pop โ†’ [1] bt(2) .... โ†’ [1,4] return, pop, pop โ†’
[2] bt(2) โ†’ [2,2] ....
โ€ปbt์— i์˜ ๊ฐ’์— ๋”ฐ๋ผ start๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์‹คํ–‰๋˜๋ฏ€๋กœ ์˜ค๋ฆ„์ฐธ์ˆœ์ผ๋•Œ๋งŒ ์‹คํ–‰๋œ๋‹ค

๐Ÿ“š์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ •๋ฆฌ

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

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๋ฌธ์œผ๋กœ ์กฐ๊ฑด์„ ๊ฑธ์–ด Back-Tracking ๊ตฌํ˜„
        if len(num)==0 or i>= num[-1]:
            num.append(i)
            bt()
            num.pop()
bt()
โ“๋‚ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ‹€๋ฆฐ ์ด์œ 

ํ‹€๋ฆฌ์ง€์•Š์•˜๋‹ค. ์ •๋‹ต์ด๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ๋‚ด ํ’€์ด๋Š” bt() ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค for loof์—์„œ if ์กฐ๊ฑด๋ฌธ์„ ๋งค๋ฒˆ ํ™•์ธํ•œ๋‹ค.
๋ฌผ๋ก  ์ด๊ฒŒ Back-Tracking์˜ ์ •์„์ด๊ธด ํ•˜๋‚˜, 
์ฒ˜์Œ๋ถ€ํ„ฐ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ถˆํ•„์š”ํ•œ ๋น„๊ต๊ณผ์ •์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
profile
keynene
post-custom-banner

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