본문 바로가기
Python/알고리즘문제

[알고스팟] 소풍

by 붕어사랑 티스토리 2022. 12. 21.
반응형
 

algospot.com :: PICNIC

소풍 문제 정보 문제 안드로메다 유치원 익스프레스반에서는 다음 주에 율동공원으로 소풍을 갑니다. 원석 선생님은 소풍 때 학생들을 두 명씩 짝을 지어 행동하게 하려고 합니다. 그런데 서로

algospot.com

해설

고등학교때 배운 조합 문제이다. 경우의 수가 중복되지 않도록 순서를 강제 해야 된다

 

import sys
C = int(input())
for _ in range(C):
    N, M = map(int, sys.stdin.readline().rstrip().split())

    flist = list(map(int, sys.stdin.readline().rstrip().split()))
    friends = [[] for _ in range(10)]
    i = 0

    while i < len(flist):
        friends[flist[i]].append(flist[i+1])
        friends[flist[i+1]].append(flist[i])
        i += 2
    haveFriend = [False]*N

    def dfs(index):
        if all(haveFriend):
            return 1
        answer = 0
        for i in range(N):
            if not haveFriend[i] and i > index:
                for f in friends[i]:
                    if not haveFriend[f] and f > i:
                        haveFriend[i] = True
                        haveFriend[f] = True
                        answer += dfs(i)
                        haveFriend[i] = False
                        haveFriend[f] = False
        return answer

    print(dfs(-1))
반응형

'Python > 알고리즘문제' 카테고리의 다른 글

[백준 2042] 구간 합 구하기  (0) 2022.12.27
[프로그래머스] 등대  (0) 2022.12.26
[알고스팟] 보글게임  (0) 2022.12.21
[알고스팟] 록 페스티벌  (0) 2022.12.20
[프로그래머스] 숫자 타자 대회  (0) 2022.12.16

댓글