728x90
https://www.acmicpc.net/problem/15655
백트래킹 연습을 위한 쉬운 문제
# import sys;input = sys.stdin.readline
def backtrack(ind=0, cnt=0):
global n, m, nums, ans_set, visit
if cnt == m:
tmp = tuple([nums[i] for i, v in enumerate(visit) if v])
if tmp not in ans_set:
ans_set.add(tmp)
print(*tmp)
return
if ind == n:
return
if not visit[ind]:
visit[ind] = 1
backtrack(ind + 1, cnt + 1)
visit[ind] = 0
backtrack(ind + 1, cnt)
def main():
global n, m, nums, ans_set, visit
n, m = map(int, input().split())
nums = sorted(map(int, input().split()))
ans_set = set()
visit = [0 for _ in range(n)]
backtrack()
if __name__ == "__main__":
main()
'STUDY > Algorithm' 카테고리의 다른 글
[백준] 1343 폴리오미노 C (0) | 2022.03.08 |
---|---|
[백준] 17471 게리맨더링 python (0) | 2022.03.07 |
[백준] 11000 강의실 배정 python (0) | 2022.02.27 |
[백준] 14502 연구소 python (0) | 2022.02.26 |
[백준] 15664 N과 M(10) python (0) | 2022.02.24 |