Skip to content

Commit a9d29e0

Browse files
committed
[BOJ]#15650,N과 M(2)/ 실버3/ 45min
https://www.acmicpc.net/problem/15650
1 parent a21bea3 commit a9d29e0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Hongjoo/백준/N과M_2.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
#15650. N과M(2)
3+
https://www.acmicpc.net/problem/15650
4+
5+
- 조합 : 1~ N 중 m개로 구성된 수열 출력하기
6+
- 사전순으로 증가하는 하는 순서로 출력
7+
- backtracking
8+
"""
9+
import sys
10+
n , m = map(int, sys.stdin.readline().split())
11+
# 1. 수열 후보군 나열하기
12+
board = list(range(n+1)) # [1,2,...n]
13+
#2. m 개의 수열 조합 출력하기
14+
stack = list()
15+
result = list()
16+
def backtracking(start , end ,m) :
17+
if len(stack) == m : # 재귀 종료 조건
18+
print(" ".join(map(str, stack)))
19+
return
20+
21+
for i in range(start, end+1) : # 모든 자식 노드 나열
22+
if i not in stack : # 선택지 유효 여부 확인 - 중복 확인
23+
stack.append(i) # 자식 노드 추가
24+
backtracking(i+1 , end , m) # 백 트래킹
25+
stack.pop() # 부모 노드로 회귀
26+
backtracking(1,n, m)

0 commit comments

Comments
 (0)