π€λ¬Έμ ν΄κ²°
-
S3 | λΈλ£¨νΈν¬μ€, λ°±νΈλνΉ
μ¬κ· ν¨μλ₯Ό μ΄μ©νμ¬ Aνμ ꡬνλ€.
-
μ 체 λ©€λ²μ μ λ°μ΄ λ λκΉμ§ νμ ꡬμ±
-
setμΌλ‘ ꡬνλ€. ( κ΅μ§ν©μ μ¬μ©ν΄μ Bνμ ꡬνκΈ° μν΄ )
λ νμ ꡬνμΌλ©΄ κ° νμ λͺ¨λ μ‘°ν©μ ν μ΄λΈμμ κ°μ κ³μ°ν΄μ ꡬν΄μ€λ€
-
μ΄λ²μλ μ½€λΉλ€μ΄μ λͺ¨λμ μ¬μ©νλ€.
-
ν μ΄λΈμ κ°μ λͺ¨λ λν΄μ£Όκ³
-
λ νμ μ΄ν©μ λΉΌμ€λ€ μ λκ°μ μμμ μ°¨μ΄λ₯Ό ꡬνλ€.
μ΅μκ°μΌλ‘ λ΅μ κ°±μ ν΄μ€λ€.
π¨ νμ ꡬν λ μ½€λΉλ€μ΄μ μ μ°μ§ μμ μ΄μ λ μ½€λΉλ€μ΄μ μ λͺ¨λ κ²½μ°μ μλ₯Ό ꡬνκΈ° λλ¬Έ
μ 체 νμ [0, 1, 2, 3] μΌ λ
A νμ΄ 0,1 Bνμ΄ 1,2 μΈ κ²½μ°μ
A νμ΄ 2,3 Bνμ΄ 0,1 μΈ κ²½μ°μ λ΅μ κ°μ§λ§
μ½€λΉλ€μ΄μ μ μ΄ λκ°μ§ κ²½μ°λ₯Ό ꡬνλ―λ‘ λΉν¨μ¨μ
π»μμ€ μ½λ
from itertools import combinations
# ν λλκΈ° - set μ΄μ©
def split_team(a_team, idx):
global answer
if len(a_team) == len(all_team) // 2:
b_team = all_team - a_team
answer = min(answer, abs(sum_stat(a_team) - sum_stat(b_team)))
return
for i in range(idx, len(all_team)):
a_team.add(i)
split_team(a_team, i + 1)
a_team.remove(i)
# ν μ€ν― κ³μ°νκΈ°
def sum_stat(team):
stat = 0
for player1, player2 in combinations(team, 2):
stat += table[player1][player2] + table[player2][player1]
return stat
if __name__ == '__main__':
N = int(input())
table = [list(map(int, input().split())) for _ in range(N)]
all_team = set(i for i in range(N))
answer = float('inf')
split_team(set(), 0)
print(answer)
πλ¬Έμ νμΈ
μΆμ²: BACKJOON ONLINE JUDGE
λ§ν¬: https://www.acmicpc.net/problem/14889
'Algorithm Problem > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[python] λ°±μ€ - 14500. ν νΈλ‘λ―Έλ Έ (μΌμ± SW μλ ν μ€νΈ κΈ°μΆ λ¬Έμ ) (0) | 2020.11.16 |
---|---|
[python] λ°±μ€ - 1261. μκ³ μ€ν (0) | 2020.11.15 |
[python] SWEA - 10726. μ΄μ§μ νν (2) | 2020.11.13 |
[python] λ°±μ€ - 14501. ν΄μ¬ (0) | 2020.11.11 |
[python] λ°±μ€ - 14235. ν¬λ¦¬μ€λ§μ€ μ λ¬Ό (0) | 2020.11.10 |