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
- Math.floor()
- 프로그래머스 숫자의 표현 java
- Algorithm
- 백준 14391
- java 내림
- 자바
- 네트워크
- 백준 15661
- java 올림
- 백준 16927
- sort
- 프로그래머스 연속된 수의 합 java
- Arrays
- 코딩테스트
- 백준 17425
- 프로그래머스 네트워크 java
- java 반올림
- java
- time complexity
- Math.ceil()
- 백준 16935
- mysql
- 0으로 채우기
- 백준 11723
- Codility
- 백준 18290
- 프로그래머스 도둑질 java
- 백준 4375
- 프로그래머스 옹알이 java
- 알고리즘
Archives
- Today
- Total
취미처럼
[백준] 15661번 링크와 스타트 본문
https://www.acmicpc.net/problem/15661
15661번: 링크와 스타트
첫째 줄에 N(4 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에 S가 주어진다. 각 줄은 N개의 수로 이루어져 있고, i번 줄의 j번째 수는 Sij 이다. Sii는 항상 0이고, 나머지 Sij는 1보다 크거나 같고, 100
www.acmicpc.net
각 팀원이 꼭 같지 않으모르 1명이상부터 모두 탐색하는 로직을 넣어야 함
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());
}
}
// 팀별로 인원이 같지 않아도 됨, 1명이상이면 팀
for(int i = 1; i <= N; i++) {
dfs(i, 0, 0);
}
System.out.println(min);
}
public static void dfs(int team, int start, int depth) {
if(team == depth) {
min = Math.min(min, diff());
return;
}
for(int i = start; i < N; i++) {
if(!visit[i]) {
visit[i] = true;
dfs(team, 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 > 백준' 카테고리의 다른 글
[백준] 10972번 다음 순열 (0) | 2021.03.04 |
---|---|
[백준] 2529번 부등호 (0) | 2021.03.04 |
[백준] 14889번 스타트와 링크 (0) | 2021.03.03 |
[백준] 14501번 퇴사 (0) | 2021.03.03 |
[백준] 1759번 암호 만들기 (0) | 2021.03.03 |
Comments