ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

6603๋ฒˆ: ๋กœ๋˜

๋ฌธ์ œ ๋…์ผ ๋กœ๋˜๋Š” {1, 2, ..., 49}์—์„œ ์ˆ˜ 6๊ฐœ๋ฅผ ๊ณ ๋ฅธ๋‹ค. ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์ „๋žต์€ 49๊ฐ€์ง€ ์ˆ˜ ์ค‘ k(k>6)๊ฐœ์˜ ์ˆ˜๋ฅผ ๊ณจ๋ผ ์ง‘ํ•ฉ S๋ฅผ ๋งŒ๋“  ๋‹ค์Œ ๊ทธ ์ˆ˜๋งŒ ๊ฐ€์ง€๊ณ  ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜๋Š”

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์ง‘ํ•ฉ์ด ์ฃผ์–ด์ง€๋ฉด ๋กœ๋˜์˜ ๊ฒฝ์šฐ 6๊ฐœ์˜ ์ˆ˜๋ฅผ ๋ฝ‘๊ธฐ ๋•Œ๋ฌธ์— 6๊ฐœ์˜ ์ˆ˜๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์˜ˆ์ œ ์ถœ๋ ฅ์„ ๋ณด๋ฉด, 6๊ฐœ๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์„ ์ฐพ๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์•ž์„œ ๋‹ค๋ฃฌ N๊ณผ M ์‹œ๋ฆฌ์ฆˆ์™€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์กฐํ•ฉ์„ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

 

์ฝ”๋“œ

DFS๋ฅผ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ ํ’€์ด

from sys import stdin


def dfs(idx, depth):
    global answer

    if depth == 6:
        answer.append([nums[i] for i in check])
    else:
        for i in range(idx, n):
            if i in check:
                continue
            check[depth] = i
            dfs(i + 1, depth + 1)
            check[depth] = -1

if __name__ == '__main__':
    while True:
        answer = []
        nums = list(map(int, stdin.readline().split()))
        if not nums[0]:
            break

        n = nums[0]
        nums = nums[1:]
        check = [-1] * 6
        dfs(0, 0)
        for num in answer:
            print(*num)
        print()

 

itertools๋ฅผ ํ™œ์šฉํ•œ ๋ฌธ์ œ ํ’€์ด

from itertools import combinations
while True:
    k, *nums = map(int, input().split())
    if not k:
        break
    for cases in combinations(nums, 6):
        for case in cases:
            print(case, end=' ')
        print()
    print()
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€