๊ด€๋ฆฌ ๋ฉ”๋‰ด

Tech Log ๐Ÿ› ๏ธ

[๋ฐฑ์ค€] 14889๋ฒˆ ์Šคํƒ€ํŠธ์™€ ๋งํฌ ์ž๋ฐ”(๋ฐฑํŠธ๋ž˜ํ‚น , ๋น„ํŠธ๋งˆ์Šคํ‚น) ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€] 14889๋ฒˆ ์Šคํƒ€ํŠธ์™€ ๋งํฌ ์ž๋ฐ”(๋ฐฑํŠธ๋ž˜ํ‚น , ๋น„ํŠธ๋งˆ์Šคํ‚น)

sehaan 2023. 3. 28. 15:08

 

๋ถ„์„


ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น , ๋น„ํŠธ๋งˆ์Šคํ‚น ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1. ๋น„ํŠธ๋งˆ์Šคํ‚น

 

๋น„ํŠธ ๋งˆ์Šคํ‚น์ด๋ž€?

๋ถˆ๋ฆฌ์–ธ ๋ฐฐ์—ด์˜ ์—ญํ• ์„ ํ•˜๋Š” ํ•˜๋‚˜์˜ ์ˆซ์ž๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋น„ํŠธ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰ , ์ˆ˜์ • ๋“ฑ์˜ ์ž‘์—…์„ ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค

- ํฐ๋Œ์˜ ํ„ฐ์ „ ์œ ํŠœ๋ธŒ

 

๋” ์ž์„ธํ•œ ์‚ฌํ•ญ์€ ์ด์ „์— ๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌํ•ด ๋†“์€ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š” 

https://cb036133.tistory.com/91

 

[๋ฐฑ์ค€] 11723 ์ง‘ํ•ฉ JAVA

๋ถ„์„ ๋น„ํŠธ๋งˆ์Šคํ‚น์„ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋น„ํŠธ๋งˆ์Šคํ‚น์ด๋ž€? - ์ปดํ“จํ„ฐ๋Š” ๋ชจ๋“  ์ž๋ฃŒ๋ฅผ ์ด์ง„์ˆ˜๋กœ ํ‘œํ˜„ํ•˜๋Š”๋ฐ, ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์“ฐ๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. ๋น„ํŠธ๋งˆ์Šคํ‚น์„ ํ†ตํ•ด ์Šค์œ„์น˜,๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ ๋“ฑ

cb036133.tistory.com

 

static void dfs() {
    for (int i = 0; i < (1 << num) ; i++) {
        int cnt = 0;
        team = new boolean[num];
        for (int j = 0; j < num; j++) {
            if ((i & (1 << j)) != 0) {
                team[j] = true;
                cnt++;
            }
        }
        if (cnt == num / 2) cal();


    }
}

์ €๋Š” ๋น„ํŠธ๋งˆ์Šคํ‚น์„ ์ด์šฉํ•ด์„œ ํŒ€์˜ ์ˆ˜๊ฐ€ ๋ฐ˜๋ฐ˜์ด ๋˜๋ฉด ๊ฐ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜ ํ•ฉ์„ ๋นผ์ฃผ๋Š” ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

2. ๋ฐฑํŠธ๋ž˜ํ‚น

๋ฐฑํŠธ๋ž˜ํ‚น์ด๋ž€?

ํ•ด๋ฅผ ์ฐพ๋Š” ๋„์ค‘ ํ•ด๊ฐ€ ์•„๋‹ˆ์–ด์„œ ๋ง‰ํžˆ๋ฉด, ๋˜๋Œ์•„๊ฐ€์„œ ๋‹ค์‹œ ํ•ด๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ธฐ๋ฒ•

 

dfs๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐฑํŠธ๋ ˆํ‚น์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

static void dfs(int idx,int depth) {
        if (depth == num / 2) {
            cal();
            return;
        } else {
            for (int i = idx; i < num; i++) {
                if (check[i] == false) {
                    check[i] = true;
                    dfs(i+1,depth + 1);
                    check[i] = false;
                }
            }
        }
    }

 

์ฝ”๋“œ - ๋น„ํŠธ๋งˆ์Šคํ‚น


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


public class Main {
    static int num;
    static int[][] arr;
    static boolean[] team;
    static int min = Integer.MAX_VALUE;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        num = Integer.parseInt(br.readLine());
        arr = new int[num][num];
        team = new boolean[num];

        for (int i = 0; i < num; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());

            for (int j = 0; j < num; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        dfs();

        System.out.println(min);
    }

    static void dfs() {
        for (int i = 0; i < (1 << num) ; i++) {
            int cnt = 0;
            team = new boolean[num];
            for (int j = 0; j < num; j++) {
                if ((i & (1 << j)) != 0) {
                    team[j] = true;
                    cnt++;
                }
            }
            if (cnt == num / 2) cal();


        }
    }

    static void cal() {
        int num1 = 0;
        int num2 = 0;

        for(int i=0;i<num-1;i++){
            for(int j=i+1;j<num;j++){
                if(team[i] == true & team[j] == true){
                    num1 += arr[i][j];
                    num1 += arr[j][i];
                }
                if(team[i] == false & team[j] == false){
                    num2 += arr[i][j];
                    num2 += arr[j][i];
                }
            }

        }


        int result = Math.abs(num1-num2);

        if(result == 0){
            System.out.println(0);
            System.exit(0);
        }

        if(min > result) min = result;
    }

}

 

์ฝ”๋“œ - ๋ฐฑํŠธ๋ž˜ํ‚น

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

import static java.lang.Math.abs;

public class Main {
    static int[][] arr;
    static boolean[] check;
    static int num;
    static int result=Integer.MAX_VALUE;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        num = sc.nextInt();
        check = new boolean[num];
        arr = new int[num][num];
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                arr[i][j] = sc.nextInt();
            }
        }

        dfs(0,0);
        System.out.println(result);
    }

    static void dfs(int idx,int depth) {
        if (depth == num / 2) {
            cal();
            return;
        } else {
            for (int i = idx; i < num; i++) {
                if (check[i] == false) {
                    check[i] = true;
                    dfs(i+1,depth + 1);
                    check[i] = false;
                }
            }
        }
    }

    static void cal() {
        int num1 = 0;
        int num2 = 0;

        for (int i = 0; i < num-1; i++) {
            for(int j= i+1;j<num;j++) {
                if (check[i] == true & check[j] == true) {
                    num1 += arr[i][j];
                    num1 += arr[j][i];
                }
                else if (check[i] == false & check[j] == false) {
                    num2 += arr[i][j];
                    num2 += arr[j][i];
                }
            }
        }
        int val = abs(num1 - num2);
        if(val == 0){
            System.out.println(0);
            System.exit(0);
        }

        result = Math.min(result,val);
    }
}