Today
Total
07-06 08:27
관리 메뉴

홍우진의 개발 일기장

[백준] 15655번: N과 M (6) / 파이썬 본문

알고리즘/백준

[백준] 15655번: N과 M (6) / 파이썬

홍우진 2025. 3. 3. 20:55
728x90
반응형

문제 링크


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

풀이 코드


n, m = map(int,input().split())
arr = sorted(list(map(int,input().split())))
li = []

def dfs(idx, cnt):
    if cnt == m:
        print(*li)
    
    for i in range(idx, n):
        idx += 1
        li.append(arr[i])
        dfs(idx, cnt + 1)
        li.pop()

dfs(0,0)

코드 해석


 백트래킹 문제다.

https://woojinhong.tistory.com/295

 

[백준] 10974번: 모든 순열 / 파이썬

문제 링크https://www.acmicpc.net/problem/10974 풀이 코드n = int(input())li = []def dfs(): if len(li) == n: print(*li) return for i in range(1, n + 1): if i not in li: li.append(i) dfs() li.pop()dfs()코드 해석파이썬 순열함수 itertools.pe

woojinhong.tistory.com

이 문제와 비슷하지만 idx와 cnt를 이용하여 시작과 카운팅을 지정했다.

 

체감 난이도: ★★★☆

 

 

 

728x90
반응형
Comments