์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[DFS] 3. ์ตœ๋Œ€์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

์‹œํ๋ฆฌํ‹ฐ์ง€ํ˜ธ 2024. 2. 22. 00:22

<<ํ’€์ด>>

 

-๋‚˜์˜ ํ’€์ด-

 

 

์ด๋ฒˆ DFS๋Š” ๋งž์ท„๋‹ค !! ใ…Žใ…Ž ๊ณ„์†ํ•˜๋‹ค๋ณด๋‹ˆ๊น ์–ด๋–ค ์›๋ฆฌ๋กœ ๋Œ์•„๊ฐ€๋Š”์ง€ ํŒŒ์•…์ด ๋˜๊ณ  ์กฐ๊ธˆ ๋” ์ˆ˜์›”ํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์ „์ฒด์ ์ธ ํ๋ฆ„

์ ‘๊ทผ ๋ฐฉ์‹์€ DFS์ด๊ณ , ์ด ์—ญ์‹œ ๋”ํ•˜๋ƒ ๋นผ๋ƒ ๋‘ ๊ฐ€์ง€๋กœ ์ตœ์ข… ๊ฐ’ 2^n ์˜ ํ˜•ํƒœ๋กœ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์šฐ์„ ์€ ๋…ธ๋“œ๋ฅผ ์—ฐ์‚ฐํ•œ ๊ฐ’์œผ๋กœ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋”ํ•˜๊ณ  ๋นผ๋Š” ๊ฒƒ์„ ๊ฐ„์„ ์œผ๋กœ ์—ฌ๊ธฐ๊ณ  ์ ‘๊ทผ์„ ํ–ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ ์‹œ๊ฐ„์ด ์˜ค๋ฒ„๊ฐ€ ๋˜๋ฉด return ์‹œ์ผฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๋ฐฐ์—ด์˜ ํ–‰ ๊ฐฏ์ˆ˜๋งŒํผ ๋Œ๋ฉด ์ตœ์ข…์ ์œผ๋กœ Math.max๋กœ 

๋น„๊ตํ•˜์—ฌ answer๋ฅผ ๊ฐฑ์‹ ํ–ˆ๋‹ค.

 

import java.util.Scanner;

class Main {
    static int answer;

    private void DFS(int L, int a, int m, int total, int[][] arr) {
        if (a > m) return;
        if (L == arr.length - 1) {
            answer = Math.max(answer, total);
            return;
        } else {
            L++;
            DFS(L, a + arr[L][1], m, total + arr[L][0], arr);
            DFS(L, a , m, total, arr);
        }
    }
    
    public static void main(String[] args) {
        Main T = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int L = 0;

        int[][] arr = new int[n + 1][2];

        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < 2; j++) {
                arr[i][j] = in.nextInt();
            }
        }

        T.DFS(L, arr[0][0], m, 0, arr);
        System.out.println(answer);
    }
}

 

 

-๊ฐ•์‚ฌ๋‹˜ ํ’€์ด-

 

import java.util.Scanner;

class Main {
    static int answer=Integer.MIN_VALUE, n, m;

    private void DFS(int L, int sum, int time, int[] ps, int[] pt) {
        if (time > m) return;
        if (L == n) {
            answer = Math.max(answer, sum);

        } else {

            DFS(L+1, sum + ps[L], time + pt[L], ps, pt);
            DFS(L+1, sum, time, ps, pt);
        }
    }

    public static void main(String[] args) {
        Main T = new Main();
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        int[] a = new int[n];
        int[] b = new int[m];

        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
            b[i] = in.nextInt();
        }


        T.DFS(0, 0, 0, a, b);
        System.out.println(answer);
    }
}

 

์—ญ์‹œ๋‚˜ ๊ฐ•์‚ฌ๋‹˜์€ ๋‚˜๋ž‘ ํ’€์ด ๋ฐฉ์‹์€ ๊ฐ™์ง€๋งŒ ํ›จ์”ฌ ๋” ์ž˜ ์ •๋ฆฌํ•ด์„œ ์ž‘์„ฑํ–ˆ๋‹ค ใ…‹ใ…‹

 

๋‚˜๋Š” 2์ฐจ์›๋ฐฐ์—ด์„ ์ƒ๊ฐํ–ˆ์ง€๋งŒ ์–ด์ฐจํ”ผ ํŠน์ • ์ธ๋ฑ์Šค์— ๋“ค์–ด๊ฐ€์•ผ ํ•  ๊ฐ’๋“ค์ด ๊ณ ์ •๋˜์–ด ์žˆ๋‹ค๋ฉด ๊ทธ๋ƒฅ 

๋ฐฐ์—ด ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์ค˜์„œ ์ˆœ์„œ์— ๋งž๊ฒŒ ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

๋‚˜๋จธ์ง€ ๋…ผ๋ฆฌ์ ์ธ ์ ‘๊ทผ ๋ฐฉ์‹์€ ๋™์ผํ•˜๋‹ค. 

 

*L++๊ณผ L + 1 ์€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, L์ž์ฒด๊ฐ€ ์ƒˆ๋กœ ๊ฐฑ์‹ ๋˜๋Š” ๊ฒƒ์€ L++๋งŒ ๊ฐ€๋Šฅํ•˜๊ณ  L + 1์€ ๋‹ค์‹œ L = L + 1์„ ํ•ด์•ผํ•œ๋‹ค.