728x90
https://www.acmicpc.net/problem/15666
백트래킹 문제 4
# N & M (12)
import sys; input = sys.stdin.readline
def backtrack(lst=None):
global N, M, nums, visit, answer
if lst is None:
lst = []
if len(lst) == M:
answer.add(tuple([nums[i] for i in lst]))
return
for i in range(N):
if lst and nums[lst[-1]] > nums[i]: continue
lst.append(i)
backtrack(lst)
lst.pop()
def main():
global N, M, nums, visit, answer
N, M = map(int, input().split())
nums = list(map(int, input().split()))
answer = set()
visit = [False for _ in range(N)]
backtrack()
print('\n'.join(map(lambda t:' '.join(map(str, t)), sorted(answer))))
if __name__ == '__main__':
main()
'STUDY > Algorithm' 카테고리의 다른 글
[백준] 14467 소가 길을 건너간 이유 1 python (0) | 2022.01.29 |
---|---|
[백준]15684 사다리 조작 C++, python (0) | 2022.01.28 |
[백준] 15663 N과 M(9) python (0) | 2022.01.27 |
[백준] 15657 N과 M(8) python (0) | 2022.01.27 |
[백준] 15654 N과 M(5) python (0) | 2022.01.27 |