본문 바로가기
Problem Solving/SWEA

[Python]1865. 동철이의 일 분배

by 부르르 2019. 5. 11.

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LuHfqDz8DFAXc

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com


 

이 문제는 일반적인 DFS로 풀면 시간초과가 발생한다.

적절한 시점에 Back Tracking 으로 가지치기를 하면(6번줄) 수행시간을 대폭 줄일 수 있다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def norm(a):
    return int(a)/100
 
def dfs(depth, prob):
    global n, answer
    if prob <= answer:
        return
    if depth == n:
        answer = max(answer, prob)
        return
    for i in range(n):
        if not v[i]:
            v[i] = True
            dfs(depth + 1, prob * p[depth][i])
            v[i] = False
 
for t in range(int(input())):
    n = int(input())
    p = [list(map(norm, input().split())) for _ in range(n)]
    v = [False] * n
    answer = 0
    dfs(0100)
    print("#{} {:.6f}".format(t+1, answer))
cs

 

 

<참고>

이 문제와 쌍둥이 문제가 있다.

7차시 5일차 - 배열 최소 합

https://www.swexpertacademy.com/main/learn/course/subjectDetail.do?subjectId=AWOVIc7KqfQDFAWg#

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

 

728x90
반응형

'Problem Solving > SWEA' 카테고리의 다른 글

[Python] 5678. 팰린드롬  (0) 2021.04.04
[Python]2616. 사업장에 흡연구역 설정하기  (0) 2019.05.11

댓글