곽로그
[백준 14889,자바] 스타트와 링크 본문
반응형
→ 개정본
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
public class Main {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static ArrayList<String> team = new ArrayList<String>();
public static int[][] capacity;
public static void main(String[] args) throws NumberFormatException, IOException {
int N = Integer.parseInt(br.readLine());
capacity = new int[N][N];
for(int i =0;i<N;i++) {
String[] line =br.readLine().split(" ");
for(int j=0;j<N;j++) {
capacity[i][j] = Integer.parseInt(line[j]);
}
}
ArrayList<Integer> candidate =new ArrayList<Integer>();
makeTeam(candidate,N);
int minGap=10000;
for(int i=0; i<team.size()/2;i++) {
String[] startTeamMember = (team.get(i)).split(" ");
String[] linkTeamMember = (team.get(team.size()-1-i)).split(" ");
int startTeamCapa=calculateCapa(startTeamMember);
int linkTeamCapa=calculateCapa(linkTeamMember);
int gap =Math.abs(startTeamCapa-linkTeamCapa);
if(gap<minGap) {
minGap = gap;
}
}
bw.write(Integer.toString(minGap));
bw.close();
}
public static void makeTeam(ArrayList<Integer> candidate, int N) throws IOException {
StringBuffer sb = new StringBuffer();
if(candidate.size()>=N/2) {
for(int i =0; i<candidate.size();i++) {
sb.append(Integer.toString(candidate.get(i))+" ");
}
team.add(sb.toString()
);
}
else {
for(int num =1; num<=N ; num++) {
if(isPromising(num,candidate)) {
candidate.add(num);
makeTeam(candidate,N);
candidate.remove(candidate.size()-1);
}
}
}
}
public static boolean isPromising(int num, ArrayList<Integer>candidate) {
for(int i =0; i<candidate.size();i++) {
if(num<=candidate.get(i)) {
return false;
}
}
return true;
}
public static int calculateCapa(String[] team) {
int total = 0;
for(int i =0;i<team.length;i++) {
int row = Integer.parseInt(team[i])-1;
for(int j=0;j<team.length;j++) {
if(i!=j) {
int column =Integer.parseInt(team[j])-1;
total += capacity[row][column];
}
}
}
return total;
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준 18290, 자바] NM과 K (1) (0) | 2020.08.12 |
---|---|
[백준 9663, 자바] N-Queen (0) | 2020.07.28 |
[백준 15649, 자바 ] N과 M (1) (0) | 2020.03.21 |
[백준 15650, 자바] N과 M (2) (0) | 2020.03.21 |
[백준 1152, 자바] 단어의 개수 (0) | 2020.03.15 |
Comments