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

728x90
๋ฐ˜์‘ํ˜•

N๊ณผ M ์‹œ๋ฆฌ์ฆˆ

  • ์›์†Œ๊ฐ€ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ
    • N๊ณผ M (5 ~ 8)
    • N๊ณผ M (1 ~ 4)์—์„œ ์›์†Œ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.
  • ์ค‘๋ณต ์›์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ
    • 15663 N๊ณผ M (9)
    • N๊ณผ M (10 ~ 12)๋Š” (9)์™€ ๊ฐ™์ด set์„ ํ™œ์šฉํ•˜๋ฉฐ ๋กœ์ง์€ N๊ณผ M 2 - 4์™€ ๋™์ผํ•˜๋‹ค.

 

๋ฌธ์ œ

 

15651๋ฒˆ: N๊ณผ M (3)

ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž์„œ ํ’€์ด ํ•œ 15649 N๊ณผ M (1)๋ฅผ ์ดํ•ดํ•˜์˜€๋‹ค๋ฉด ์ด ๋ฌธ์ œ๋Š” ๋” ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค. ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋Š” ์ˆœ์—ด์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ ์ค‘๋ณต์„ ์ฒดํฌํ•˜๋Š” ๊ตฌ๋ฌธ์„ ์‚ญ์ œ ํ•ด์ฃผ๋ฉด N๊ฐœ์˜ ์ˆ˜์— ๋Œ€ํ•œ M ๊ธธ์ด์˜ ์ค‘๋ณต ์ˆœ์—ด์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค

 

์ฝ”๋“œ

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

from sys import stdin


def dfs(depth):
    global answer
    # ์žฌ๊ท€ ์ข…๋ฃŒ ์กฐ๊ฑด → ์„ ํƒํ•˜๊ณ ์ž ํ•˜๋Š” ์ˆ˜
    if depth == m:
        answer.append([*check])
    else:
        for i in range(n):
            # ๋ณ„๋„์˜ ์ค‘๋ณต ํ™•์ธ์ด ํ•„์š” ์—†์Œ
            check[depth] = i + 1
            dfs(depth + 1)

if __name__ == '__main__':
    answer = []
    n, m = map(int, stdin.readline().split())
    check = [0] * m
    dfs(0)

    for num in answer:
        print(*num)

 

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

from itertools import product

N, M = map(int, input().split())
nums = [num + 1 for num in range(N)]
for cases in product(nums, repeat=M):
    for case in cases:
        print(case, end=' ')
    print()
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€