[BaekJoon] ๋ฐฑ์ค€ 15654๋ฒˆ : N๊ณผ M (5)

Updated:

15654๋ฒˆ : N๊ณผ M (5)


[BaekJoon] ๋ฐฑ์ค€ 15652๋ฒˆ : N๊ณผ M (4) ์™€ ๊ฐ™์€ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๊ฐ€์ง€๋ฅผ ๋ป—์ณ๊ฐ€๋ฉฐ ๋…ธ๋“œ๋ฅผ ๊ณ„์† ๋ฐฉ๋ฌธํ•˜๋‹ค๊ฐ€ ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์œผ๋ฉด ๋ถ€๋ชจ ๋…ธ๋“œ๋กœ ๋˜๋Œ์•„๊ฐ€ ๋‹ค๋ฅธ ๊ณณ์„ ๋ฐฉ๋ฌธํ•œ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ์ˆ˜์—ด์— ์ˆœ์„œ๊ฐ€ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ์ˆœ์—ด๋กœ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.


import sys


def DFS():
	global n, m, data, stack

	if len(stack) == m:
		print(*stack)
		return

	for i in range(n):
		stack.append(data[i])

		if len(set(stack)) != len(stack):
			stack.pop()
		else:
			DFS()
			stack.pop()


n, m = map(int, sys.stdin.readline().rsplit())
data = sorted(list(map(int, sys.stdin.readline().rsplit())))
stack = []
DFS()

import sys
from itertools import permutations


n, m = map(int, sys.stdin.readline().rsplit())
data = sorted(list(map(int, sys.stdin.readline().rsplit())))
res = permutations(data, m)

for val in res:
	print(*val)

์ˆœ์—ด์„ ๋งŒ๋“ค๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— permutation ์„ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.



Categories:

Updated:

Leave a comment