https://www.acmicpc.net/problem/14889
코드
from itertools import combinations
n = int(input())
ability = [list(map(int, input().split())) for _ in range(n)]
ans = int(1e9)
for start in combinations(range(n), n//2):
start = set(start)
link = set(range(n))-start
# 스타트팀 능력치
start_ability = 0
for comb in combinations(list(start), 2):
start_ability += (ability[comb[0]][comb[1]]+ability[comb[1]][comb[0]])
# 링크팀 능력치
link_ability = 0
for comb in combinations(list(link), 2):
link_ability += (ability[comb[0]][comb[1]]+ability[comb[1]][comb[0]])
ans = min(ans, abs(start_ability-link_ability))
print(ans)
'문제풀이 > 완전탐색' 카테고리의 다른 글
[Python/파이썬] 백준 21943번 연산 최대로 (0) | 2024.06.03 |
---|---|
[Python/파이썬] 백준 1254번 팰린드롬 만들기 (0) | 2024.05.31 |
[Python/파이썬] 백준 28075번 스파이 (0) | 2024.03.15 |
[Python/파이썬] 백준 2503번 숫자 야구 (0) | 2024.02.09 |
[Python/파이썬] 백준 5883번 아이폰 9S (0) | 2024.02.06 |
https://www.acmicpc.net/problem/14889
코드
from itertools import combinations n = int(input()) ability = [list(map(int, input().split())) for _ in range(n)] ans = int(1e9) for start in combinations(range(n), n//2): start = set(start) link = set(range(n))-start # 스타트팀 능력치 start_ability = 0 for comb in combinations(list(start), 2): start_ability += (ability[comb[0]][comb[1]]+ability[comb[1]][comb[0]]) # 링크팀 능력치 link_ability = 0 for comb in combinations(list(link), 2): link_ability += (ability[comb[0]][comb[1]]+ability[comb[1]][comb[0]]) ans = min(ans, abs(start_ability-link_ability)) print(ans)
'문제풀이 > 완전탐색' 카테고리의 다른 글
[Python/파이썬] 백준 21943번 연산 최대로 (0) | 2024.06.03 |
---|---|
[Python/파이썬] 백준 1254번 팰린드롬 만들기 (0) | 2024.05.31 |
[Python/파이썬] 백준 28075번 스파이 (0) | 2024.03.15 |
[Python/파이썬] 백준 2503번 숫자 야구 (0) | 2024.02.09 |
[Python/파이썬] 백준 5883번 아이폰 9S (0) | 2024.02.06 |