본문 바로가기
Algorithm

[백준] 15649번 : N과 M (1) (파이썬)

by 이은선 2024. 1. 10.
728x90
SMALL

백트래킹 문제 

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

 

15649번: N과 M (1)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net

 

 N과 M  (1)  

# N과 M (1) (실버 3)
import sys
n, m = map(int, sys.stdin.readline().split())
result = [0] * m
arr = [i + 1 for i in range(n)]
checked = [False for _ in range(n)]


def dfs(level):
    if level == m:
        print(*result)
        return

    for i in range(n):
        if checked[i] == True: continue
        result[level] = arr[i]
        checked[i] = True
        dfs(level + 1)
        checked[i] = False
dfs(0)

 

 

*참고

https://esssun.tistory.com/52

 

순열, 조합, 중복순열, 중복조합

순열 [1,2,3] 중 2개를 뽑아 순열을 만드는 예시 def dfs(level): if level == r: print(result) return for i in range(len(arr)): if checked[i] == True: continue result[level] = arr[i] checked[i] = True # 중복순열이 안되게 -> 앞자릿수

esssun.tistory.com

728x90
LIST

'Algorithm' 카테고리의 다른 글

백준 자바 제출 방법  (1) 2023.07.23