728x90
반응형

import sys
input = sys.stdin.readline
# 1 ~ N 까지의 수, M자리
N, M = map(int, input().split())
def compare(a, b):
if a > b:
return False
return True
def backTracking(depth, num):
if depth == M:
print(' '.join(num))
return
for i in range(1, N + 1):
if depth == 0 or compare(int(num[-1]), i):
backTracking(depth + 1, num + str(i))
backTracking(0, '')
앞선 N과 M 문제들과 큰 차이가 없다.
모든 수열을 만드는 backTracking 함수를 작성한다.
뒤에 오는 숫자가 같거나 큰지 검사하는 compare함수를 작성한다.
이후, depth가 0이거나 compare 함수 조건을 만족하면 수열에 숫자를 추가하며, depth가 M에 도달했을 경우 해당 수열에 공백을 추가해 출력한다.
728x90
반응형
'Algorithm > BackTracking' 카테고리의 다른 글
[Baekjoon 15655 / Python / 실버3] N과 M (6) (0) | 2024.08.07 |
---|---|
[Baekjoon 15654 / Python / 실버3] N과 M (5) (0) | 2024.08.07 |
[Baekjoon 15651 / Python / 실버3] N과 M (3) (0) | 2024.08.07 |
[Baekjoon 15650 / Python / 실버3] N과 M (2) (0) | 2024.08.07 |
[Baekjoon 2529 / Python / 실버1] 부등호 (0) | 2024.08.06 |