728x90
SMALL
백트래킹 문제
https://www.acmicpc.net/problem/15650
15650번: N과 M (2)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
N과 M (2)
# N과 M (2) (실버 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, begin):
if level == m:
print(*result)
return
for i in range(begin, n):
if checked[i] == True: continue
result[level] = arr[i]
checked[i] = True
dfs(level + 1, i + 1)
checked[i] = False
dfs(0, 0)
*참고
순열, 조합, 중복순열, 중복조합
순열 [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 > Back Tracking' 카테고리의 다른 글
[소프티어] Lv.3 함께하는 효도 (파이썬) (0) | 2025.01.28 |
---|---|
[소프티어] HSAT 2회 정기 코딩 인증평가 기출 : 사물인식 최소 면적 산출 프로그램 (파이썬) (1) | 2025.01.26 |