Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Arrays
- 0으로 채우기
- java 반올림
- mysql
- 백준 16935
- 백준 11723
- Math.ceil()
- 백준 14391
- time complexity
- 알고리즘
- 프로그래머스 네트워크 java
- 프로그래머스 도둑질 java
- 코딩테스트
- java 내림
- 네트워크
- 프로그래머스 옹알이 java
- 백준 16927
- 자바
- 백준 18290
- sort
- 프로그래머스 연속된 수의 합 java
- java 올림
- 백준 17425
- 프로그래머스 숫자의 표현 java
- 백준 15661
- 백준 4375
- Codility
- Algorithm
- Math.floor()
- java
Archives
- Today
- Total
취미처럼
[백준] 14889번 스타트와 링크 본문
https://www.acmicpc.net/problem/14889
14889번: 스타트와 링크
예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다.
www.acmicpc.net
import java.util.*;
import java.io.*;
public class Main {
public static int N;
public static boolean[] visit;
public static int[][] arr;
public static int min = Integer.MAX_VALUE;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
visit = new boolean[N];
arr = new int[N][N];
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < N; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
dfs(0, 0);
System.out.println(min);
}
public static void dfs(int start, int depth) {
// 팀을 나누기 위해 depth는 N / 2 까지
if(N/2 == depth) {
min = Math.min(min, diff());
return;
}
// 중복방지를 위해 start를 올려줌
for(int i = start; i < N; i++) {
if(!visit[i]) {
visit[i] = true;
dfs(i + 1, depth+1);
visit[i] = false;
}
}
}
// 스타트와 링크의 차이를 구한다.
public static int diff() {
int start = 0; // visit = true
int link = 0; // visit = false
for(int i = 0; i < N - 1; i++) {
for(int j = i + 1; j < N ; j++) {
if(visit[i] && visit[j]) {
start += arr[i][j] + arr[j][i];
} else if(!visit[i] && !visit[j]) {
link += arr[i][j] + arr[j][i];
}
}
}
return Math.abs(start-link);
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 2529번 부등호 (0) | 2021.03.04 |
---|---|
[백준] 15661번 링크와 스타트 (0) | 2021.03.03 |
[백준] 14501번 퇴사 (0) | 2021.03.03 |
[백준] 1759번 암호 만들기 (0) | 2021.03.03 |
[백준] 18290번 NM과 K (1) (0) | 2021.03.03 |
Comments