๐Ÿ“– [๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด] Q.14889 ์Šคํƒ€ํŠธ์™€ ๋งํฌ ๋ฌธ์ œ ํ’€์ด - java

๐Ÿ“– ๋ฌธ์ œ

https://www.acmicpc.net/problem/14889

๐Ÿ” ์ ‘๊ทผ๋ฒ•

Q.14889 ์Šคํƒ€ํŠธ์™€ ๋งํฌ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ง์ˆ˜์ธ N๋ช…์„ ๋‘ํŒ€์œผ๋กœ ๋‚˜๋ˆ ์„œ

ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๊ฐ€ ๊ฐ€์žฅ ๊ณตํ‰ํ•œ๊ฒฝ์šฐ๋ฅผ (ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์˜ ์ฐจ์ด๋ฅผ ์ตœ์†Œํ•œ์œผ๋กœ)

์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

i๋ฒˆ๊ณผ j๋ฒˆ์ด ํŒ€์ด ๋˜์—ˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ

๋Šฅ๋ ฅ์น˜ ability[i][j] ์™€ ability[j][i] ๊ฐ€ ๊ฐ๊ฐ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ๋”ฐ์ ธ๋ณด๋ฉด ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์˜ ์ฐจ์ด๊ฐ€ ์ตœ์†Œ์ธ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์กฐํ•ฉ์„ ์ด์šฉํ•ด

N๋ช… ์ค‘์— N/2๋ช…์„ ๊ณจ๋ผ ํ•œ ํŒ€์„ ๋งŒ๋“ค๋ฉด ๋‚˜๋จธ์ง€ N/2๋ช…์ด ํ•œ ํŒ€์ด ์ด๋ค„์ง‘๋‹ˆ๋‹ค.

์ด๋ฅผ ์ด์šฉํ•ด ํŒ€์„ ๋‚˜๋ˆ„๊ณ 

ํŒ€์ด ๋‚˜๋ˆ ์ง€๋ฉด ๋‚˜๋ˆ ์ง„ ๊ฐ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ

์ตœ์†Œ์ธ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์œผ๋ฉด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’ป ์ฝ”๋“œ


package problem.brute;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main_14889 {

    static int sum[] = new int[2];
    static int n;
    static int[][] ability;
    static boolean [] visited;
    static int min = Integer.MAX_VALUE;
    static List<Integer>[] teamList = new List[2];

    public static void main(String[] args) throws IOException {
        input();
        dfs(1,1);
        System.out.println(min);
    }


    public static void input() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        ability = new int[n+1][n+1];
        visited = new boolean[n+1];
        for(int i=1; i<=n; i++){
            String temp[] = br.readLine().split( " ");
            for(int j=1; j<=n; j++){
                ability[i][j] = Integer.parseInt(temp[j-1]);
            }
        }
    }

    public static void dfs(int depth,int index) {
        if(min == 0){
            return;
        }
        if((n/2)+1==depth){
            divideTeam();
            caculate();
            return;
        }
        for(int i=index; i<=n; i++){
            if(!visited[i]){
                visited[i] = true;
                dfs(depth+1,i);
                visited[i] = false;
            }
        }
    }

    public static void divideTeam(){
        teamList[0] = new ArrayList<Integer>();
        teamList[1] = new ArrayList<Integer>();
        for(int i=1; i<=n; i++) {
            if(visited[i]){
                teamList[0].add(i);
            }
            else
                teamList[1].add(i);

        }
    }
    public static void caculate(){
        for(int index=0; index<2; index++){
            sum[index] = 0;
            for(int i=0; i<teamList[index].size(); i++){
                int x = teamList[index].get(i);
                for(int j=0; j<teamList[index].size(); j++){
                    int y = teamList[index].get(j);
                    if(x==y){
                        continue;
                    }
                    sum[index] += ability[x][y];
                }
            }
        }
        int result = Math.abs(sum[0]-sum[1]);
        min = Math.min(min,result);
    }
}


Written on March 8, 2021