[BOJ] #15649) N๊ณผ M(1) (Python, Back tracking)

    ๋ฐ˜์‘ํ˜•

    ๐ŸŒฑ ๋ฌธ์ œ

     

    15649๋ฒˆ: N๊ณผ M (1)

    ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด

    www.acmicpc.net

    https://www.acmicpc.net/problem/15649

    • 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ ์ค‘๋ณต ์—†์ด M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ์ถœ๋ ฅ
    • ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€

     

    ๐ŸŒฑ ํ’€์ด 1) ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ

    ๋ฐฑํŠธ๋ž™ํ‚น ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ๋ผ๊ณ  ํ•ด์„œ ํ’€์–ด๋ณธ๊ฑด๋ฐ ๋ฌธ์ œ ์ฝ์ž๋งˆ์ž ๊ทธ๋ƒฅ permutations ์“ฐ๋ฉด ๋˜๋Š” ๊ฒƒ ์•„๋‹˜ ? ! ?

    ๊ทธ๋ž˜์„œ ์ˆœ์—ด ๋ฝ‘์•„์ฃผ๋Š” permutations ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์จ์„œ ์ œ์ถœํ•˜๋‹ˆ๊นŒ ์ •๋‹ต,, ๐Ÿ’™ 

    from itertools import permutations
    
    n, m = map(int, input().split())
    
    num_list = [_ for _ in range(1, n + 1)]
    
    for p in permutations(num_list, m):
        print(*p)

    ๊ฒฐ๋ก ์€ ํŒŒ์ด์ฌ ๋„ˆ๊ฐ€ ์งฑ (?)

     

     

    ๐ŸŒฑ ํ’€์ด 2) ๋ฐฑํŠธ๋ž™ํ‚น

    ๊ทธ๋ž˜๋„ ๋ฐฑํŠธ๋ž™ํ‚น ๋ฌธ์ œ์ด๋‹ˆ ๋ฐฑํŠธ๋ž™ํ‚น์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด…์‹œ๋‹ค. ๋ฐฑํŠธ๋ž™ํ‚น์€ ํ˜„์žฌ ์ƒํƒœ์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ›„๋ณด๊ตฐ์„ ๋”ฐ๋ผ ๋“ค์–ด๊ฐ€๋ฉฐ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

    N๊ณผ M(1) ๋ฌธ์ œ์˜ ์ƒํƒœ ๊ณต๊ฐ„ ํŠธ๋ฆด๋ฅด ๊ฐ„๋žตํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ด๋ฉด ์œ„์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    answer = []
    
    
    def backtracking():
        if len(answer) == m:
            print(" ".join(map(str, answer)))
            return
        for i in range(1, n + 1):
            if i not in answer:
                answer.append(i)
                backtracking()
                answer.pop()
    
    
    backtracking()
    • ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ํ•ด๋‹น ์ˆซ์ž๊ฐ€ ๋ฐฐ์—ด(answer)์— ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์„ ์ฑ„์›๋‹ˆ๋‹ค.
    • ๊ทธ๋ฆฌ๊ณ  backtracking() ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ backtracking() ์žฌ๊ท€ ํ•จ์ˆ˜๋Š” ์ •๋‹ต์„ ๋งŒ์กฑ์‹œํ‚ค๋ฉด returnํ•˜๋„๋ก ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
    • backtracking()์—์„œ return๋˜์ง€ ์•Š์œผ๋ฉด ๋‹ค์‹œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ๋˜ answer์„ ์ฑ„์šธ๊ฒ๋‹ˆ๋‹ค.
    • ๋งŒ์•ฝ ๋ฐฐ์—ด์ด ๋‹ค ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด backtracking()์—์„œ return์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด answer.pop()์„ ํ†ตํ•ด ๋ฐฐ์—ด์ด ์ด์ „ ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

     

    ๐ŸŒฑ ์ •๋‹ต ์ฝ”๋“œ

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    # boj 15649 N๊ณผ M(1)
    
    from itertools import permutations
    
    n, m = map(int, input().split())
    
    # ํ’€์ด 1 (ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ)
    num_list = [_ for _ in range(1, n + 1)]
    
    for p in permutations(num_list, m):
        print(*p)
    
    
    # ํ’€์ด 2 (๋ฐฑํŠธ๋ž™ํ‚น)
    answer = []
    
    
    def backtracking():
        if len(answer) == m:
            print(" ".join(map(str, answer)))
            return
        for i in range(1, n + 1):
            if i not in answer:
                answer.append(i)
                backtracking()
                answer.pop()
    
    
    backtracking()

     

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€