[백준 15663번] N과 M(9)

박형진·2023년 2월 22일
0

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


1. 코드

import sys
from collections import defaultdict


def dfs(cnt, path):
    if cnt == m and path:
        result.add(tuple(path))
        return

    for i in range(len(arr)):
        if not visited[i]:
            visited[i] = True
            dfs(cnt+1, path + [arr[i]])
            visited[i] = False


result = set()
n, m = map(int, sys.stdin.readline().rstrip().split())
arr = list(map(int, sys.stdin.readline().rstrip().split()))
visited = defaultdict(bool)

dfs(0, [])
for i in sorted(result):
    print(*i)

2. 후기

순열 구현

profile
안녕하세요!

0개의 댓글