728x90
https://www.acmicpc.net/problem/15666
15666번: N과 M (12)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
백트래킹 문제 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 |