Baekjoon/백트래킹
[백준] 15655 N과 M (6) (실버3) / 백트래킹
hellosonic
2023. 4. 14. 10:23
문제요약
나의 코드 및 설명 - 방문처리 빠짐
- 고른 숫자들이 오름차순으로 정렬되기 위해 for문의 시작점을 start로 설정해두고, 하위 함수를 호출할 때 시작점을 변수로 받는다.
def dfs(count, start = 0):
if count == m:
print(" ".join(map(str, ans)))
return
if count == n:
return
for i in range(start, n):
ans.append(num_list[i])
dfs(count+1, i+1)
ans.pop()
n, m = map(int, input().split())
num_list = list(map(int, input().split()))
num_list.sort()
ans = []
dfs(0)
나의 코드 및 설명 - 방문처리 넣음
def dfs(count, start):
if count == m:
print(" ".join(map(str, ans)))
return
if count == n:
return
for i in range(start, n):
if visited[i] == 0:
visited[i] = 1
ans.append(num_list[i])
dfs(count+1, i+1)
ans.pop()
visited[i] = 0
n, m = map(int, input().split())
num_list = list(map(int, input().split()))
num_list.sort()
ans = []
visited = [0] * n
dfs(0,0)