๊ธฐ๋ก๋ฐฉ

BOJ_14889 : ์Šคํƒ€ํŠธ์™€ ๋งํฌ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_14889 : ์Šคํƒ€ํŠธ์™€ ๋งํฌ

Soom_1n 2023. 4. 7. 15:38

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

14889๋ฒˆ: ์Šคํƒ€ํŠธ์™€ ๋งํฌ

์˜ˆ์ œ 2์˜ ๊ฒฝ์šฐ์— (1, 3, 6), (2, 4, 5)๋กœ ํŒ€์„ ๋‚˜๋ˆ„๋ฉด ๋˜๊ณ , ์˜ˆ์ œ 3์˜ ๊ฒฝ์šฐ์—๋Š” (1, 2, 4, 5), (3, 6, 7, 8)๋กœ ํŒ€์„ ๋‚˜๋ˆ„๋ฉด ๋œ๋‹ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • N๋ช… ์ค‘์—์„œ N/2๋ช…์„ ๋ฝ‘์•˜์„๋•Œ ๋‚˜์˜ค๋Š” ์‹œ๋„ˆ์ง€ ์ฐจ์ด์˜ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • N๋ช…์—์„œ N/2๋ฅผ ๋ฝ‘๋Š” ์กฐํ•ฉ ๋ฌธ์ œ์ด๋‹ค.
    • ๋ฐ˜๋Œ€ ํŒ€์œผ๋กœ ๋ฝ‘ํžˆ๋ฉด ๊ฐ™์€ ๊ฐ’์„ ์ค‘๋ณต์œผ๋กœ ๊ฒ€์‚ฌํ•˜๊ฒŒ ๋˜์ง€๋งŒ, ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํ—ˆ์šฉ๋ฒ”์œ„ ๋‚ด์ด๋ฏ€๋กœ ๊ฐ์•ˆํ•ด๋„ ๋œ๋‹ค.
    • ์กฐํ•ฉ์œผ๋กœ ๋ฝ‘์•„ ์‹œ๋„ˆ์ง€ ์ฐจ์ด์˜ ์ตœ์†Œ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

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

public class Main {
    private static int N, ans = Integer.MAX_VALUE;
    private static int[][] arr;
    private static boolean[] visited;

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        arr = new int[N][N];
        visited = new boolean[N];

        StringTokenizer st = null;
        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());
            }
        }

        // Combination
        comb(0, 0, 0);

        // Output
        System.out.println(ans);
    }

    private static boolean comb(int start, int cnt, int sum) {
        if (cnt == N / 2) {
            int temp = 0;
            for (int i = 0; i < N-1; i++) {
                for (int j = i+1; j < N; j++) {
                    if (!visited[i] && !visited[j])
                        temp += arr[i][j] + arr[j][i];
                }
            }

            ans = Math.min(ans, Math.abs(sum - temp));
            return ans == 0;
        }

        for (int i = start; i < N; i++) {
            if (!visited[i]) {
                visited[i] = true;

                int temp = 0;
                for (int j = 0; j < i; j++) {
                    if (visited[j])
                        temp += arr[i][j] + arr[j][i];
                }

                if(comb(i + 1, cnt + 1, sum + temp)) return true;

                visited[i] = false;
            }
        }
        return false;
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์žฌ๊ท€ ๋ฉ”์„œ๋“œ comb๋ฅผ ์ด์šฉํ•ด ์กฐํ•ฉ์„ ๊ตฌํ˜„ํ•œ๋‹ค.
    • ํŒ€์›์„ ๊ณ ๋ฅผ ๋•Œ ๋งˆ๋‹ค ์‹œ๋„ˆ์ง€ ๊ฐ’์„ ๋ˆ„์ ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
    • ํŒ€์›์„ ๋‹ค ๊ณจ๋ž์œผ๋ฉด ๋ฐ˜๋Œ€์ชฝ ํŒ€์›๋“ค์˜ ์‹œ๋„ˆ์ง€ ๊ฐ’ ์ดํ•ฉ๋„ ๊ณ„์‚ฐํ•œ๋‹ค.
    • ์‹œ๋„ˆ์ง€ ๊ฐ’ ์ฐจ์ด๋ฅผ ans์— ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
  • ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•˜๊ณ  ans๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋– ์„œ ๊ณ„์‚ฐ๊ฐ’์„ ์žฌ๊ท€ ๋ฉ”์„œ๋“œ์—์„œ sum์œผ๋กœ ๋“ค๊ณ ๋‹ค๋…”๋Š”๋ฐ, ์‚ฌ์‹ค ํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ  ๋‹ค๋ฅธ ๋ถ€๋ถ„์—์„œ ๊ตฌํ˜„ ์‹ค์ˆ˜๊ฐ€ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

728x90