Skip to content

Commit 4728aa6

Browse files
committed
issue #42 14889 스타트와 링크
1 parent 44e6181 commit 4728aa6

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

src/backjoon/_14889.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,87 @@
44
import java.io.BufferedReader;
55
import java.io.IOException;
66
import java.io.InputStreamReader;
7+
import java.util.StringTokenizer;
78

89
public class _14889 {
10+
public static int N; // 주어진 숫자 개수
11+
public static int[][] skill; // 능력치
12+
static boolean[] visit;
13+
static int Min = Integer.MAX_VALUE;
14+
915
public static void main(String[] args) throws IOException {
1016
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11-
// memory runtime
17+
// memory 15696 runtime 324
18+
N = Integer.parseInt(br.readLine());
19+
skill = new int[N][N];
20+
visit = new boolean[N];
21+
22+
// 숫자들 배열에 넣기
23+
for (int i = 0; i < N; i++) {
24+
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
25+
for(int j=0; j<N; j++){
26+
skill[i][j] = Integer.parseInt(st.nextToken());
27+
}
28+
}
29+
combination(0, 0);
30+
System.out.println(Min);
31+
}
32+
33+
static void combination(int idx, int cnt){
34+
// 팀 조합이 완성된 경우
35+
if(cnt == N/2){
36+
diff();
37+
return;
38+
}
39+
40+
for(int i=idx; i<N; i++){
41+
// 방문하지않은 경우
42+
if(!visit[i]){
43+
visit[i] = true;
44+
combination(i+1, cnt+1); // 재귀
45+
visit[i] = false;
46+
}
47+
}
48+
}
49+
// 두 팀의 능력치 차이를 계산하는 함수
50+
static void diff(){
51+
int team_start = 0;
52+
int team_link = 0;
53+
54+
for(int i=0; i<N-1; i++){
55+
for(int j=i+1; j<N; j++){
56+
if(visit[i] == true && visit[j] == true){
57+
team_start += skill[i][j];
58+
team_start += skill[j][i];
59+
}
60+
// i 번째 사람과 j 번째 사람이 false라면 링크팀으로 점수 플러스
61+
else if (visit[i] == false && visit[j] == false) {
62+
team_link += skill[i][j];
63+
team_link += skill[j][i];
64+
}
65+
}
66+
}
67+
// 두 팀의 점수 차이 (절댓값)
68+
int val = Math.abs(team_start - team_link);
69+
70+
// 두 팀의 차이가 0이라면 가낭 낮은 최솟값이기때문에 종료
71+
if (val == 0) {
72+
System.out.println(val);
73+
System.exit(0);
74+
}
75+
Min = Math.min(val, Min);
1276
}
1377
}
78+
/*
79+
input
80+
6
81+
0 1 2 3 4 5
82+
1 0 2 3 4 5
83+
1 2 0 3 4 5
84+
1 2 3 0 4 5
85+
1 2 3 4 0 5
86+
1 2 3 4 5 0
87+
88+
output
89+
2
90+
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy