백준 15651 N과 M (3)

김민영·2023년 1월 6일
0

알고리즘

목록 보기
32/125

과정

  • 백트래킹 공부하기
  • 1부터 N까지 값을 리스트로 만들어놓음.
  • result라는 빈 리스트에 숫자를 하나씩 추가함.
  • 숫자를 추가하는 경우와 숫자를 추가하지 않는 경우를 가지처럼 만들어 나감.
N, M = map(int, input().split())
lst = [i for i in range(1, N+1)]
result = []


def back(M, N, level, idx):
    if level == M:
        print(*result)
        return
    for i in range(idx, N):
        result.append(lst[i])
        back(M, N, level + 1, idx)
        result.pop(-1)

back(M, N, 0, 0)
  • 함수의 반복문 안에서 추가한 경우와 추가하지 않는 경우가 갈림.
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글