Skip to content

Commit 2809c04

Browse files
committed
[BruteForce] baekjoon-14889
1 parent a7cc53c commit 2809c04

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
| 02 | | [Programmers ์นดํŽซ](./src/BruteForce/prg42842) | |
6363
| 03 | | [Baekjoon-2961 ๋„์˜์ด๊ฐ€ ๋งŒ๋“  ๋ง›์žˆ๋Š” ์Œ์‹](./src/BruteForce/P2961) | |
6464
| 04 | | [Baekjoon-3040 ๋ฐฑ์„ค ๊ณต์ฃผ์™€ ์ผ๊ณฑ ๋‚œ์Ÿ์ด](./src/BruteForce/P3040) | |
65+
| 05 | | [Baekjoon-14889 ์Šคํƒ€ํŠธ์™€ ๋งํฌ](./src/BruteForce/P14889) | ์‚ผ์„ฑ SW ์—ญ๋Ÿ‰ ํ…Œ์ŠคํŠธ ๊ธฐ์ถœ |
6566

6667
## DFS & BFS
6768

โ€Žsrc/BruteForce/P14889/Main.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package BruteForce.P14889;
2+
3+
import java.io.*;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
8+
static int N, ans = Integer.MAX_VALUE;
9+
static int[][] S;
10+
static boolean[] visited;
11+
12+
public static void main(String[] args) throws Exception {
13+
System.setIn(new FileInputStream("src/BruteForce/P14889/input.txt"));
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
N = Integer.parseInt(br.readLine());
17+
S = new int[N+1][N+1];
18+
for (int i = 1; i <= N; i++) {
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
for (int j = 1; j <= N; j++) S[i][j] = Integer.parseInt(st.nextToken());
21+
}
22+
23+
visited = new boolean[N+1];
24+
visited[1] = true;
25+
comb(2, 1);
26+
27+
System.out.println(ans);
28+
}
29+
30+
static void comb(int s, int cnt) {
31+
if (cnt == N/2) {
32+
calc();
33+
return;
34+
}
35+
36+
for (int i = s; i <= N; i++) {
37+
visited[i] = true;
38+
comb(i+1, cnt+1);
39+
visited[i] = false;
40+
}
41+
}
42+
43+
static void calc() {
44+
int s1 = 0, s2 = 0;
45+
int[] t1 = new int[N/2], t2 = new int[N/2];
46+
47+
for (int i = 1, k = 0, l = 0; i <= N; i++) {
48+
if (visited[i]) t1[k++] = i;
49+
else t2[l++] = i;
50+
}
51+
52+
for (int i = 0; i < N/2-1; i++) {
53+
for (int j = i; j < N/2; j++) {
54+
s1 += S[t1[i]][t1[j]] + S[t1[j]][t1[i]];
55+
s2 += S[t2[i]][t2[j]] + S[t2[j]][t2[i]];
56+
}
57+
}
58+
59+
ans = Math.min(ans, Math.abs(s1-s2));
60+
}
61+
}

โ€Žsrc/BruteForce/P14889/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## [baekjoon-14889] ์Šคํƒ€ํŠธ์™€ ๋งํฌ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/107963418-7cfec080-6feb-11eb-9bee-fd59e8aca41e.png)
4+
![image](https://user-images.githubusercontent.com/22045163/107963481-943dae00-6feb-11eb-8afc-6a0914915b71.png)
5+
6+
![image](https://user-images.githubusercontent.com/22045163/107963511-9dc71600-6feb-11eb-91f3-5a20c2de140f.png)

โ€Žsrc/BruteForce/P14889/input.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
6
2+
0 1 2 3 4 5
3+
1 0 2 3 4 5
4+
1 2 0 3 4 5
5+
1 2 3 0 4 5
6+
1 2 3 4 0 5
7+
1 2 3 4 5 0

0 commit comments

Comments
ย (0)