We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent dc3537d commit 2fdb5bdCopy full SHA for 2fdb5bd
minjeong/Backtracking/2024-03-15-[백준]-#14889-스타트와 링크.py
@@ -0,0 +1,36 @@
1
+import sys
2
+input = sys.stdin.readline
3
+
4
+N = int(input())
5
+S = [list(map(int, input().split())) for _ in range(N)]
6
+check = [False] * N
7
+minValue = 1000000000
8
9
+def calculate():
10
+ global minValue
11
+ Ateam = 0
12
+ Bteam = 0
13
+ for i in range(N):
14
+ for j in range(N):
15
+ if check[i] and check[j]:
16
+ Ateam += S[i][j]
17
+ if not check[i] and not check[j]:
18
+ Bteam += S[i][j]
19
20
+ minValue = min(minValue, abs(Ateam - Bteam))
21
22
+def backtracking(start, size):
23
24
+ if size == N//2:
25
+ calculate()
26
+ return
27
28
+ for i in range(start, N):
29
+ if check[i] == False:
30
+ check[i] = True
31
+ backtracking(i+1, size+1)
32
+ check[i] = False
33
34
35
+backtracking(0, 0)
36
+print(minValue)
0 commit comments