์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch08. DFS, BFS ํ™œ์šฉ : ์ตœ๋Œ€์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ DFS

2023. 12. 6. 14:08ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

 

https://hyejin.tistory.com/1276

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch08. DFS, BFS ํ™œ์šฉ : ๋ฐ”๋‘‘์ด ์Šน์ฐจ DFS

https://hyejin.tistory.com/1266 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch08. DFS, BFS ํ™œ์šฉ : ํ•ฉ์ด ๊ฐ™์€ ๋ถ€๋ถ„ ์ง‘ํ•ฉ (DFS, ์•„๋งˆ์กด ์ธํ„ฐ๋ทฐ) https://hyejin.tistory.com/1265 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree, Graph(

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

 

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

์„ค๋ช…

์ด๋ฒˆ ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ๋Œ€ํšŒ์—์„œ ์ข‹์€ ์„ฑ์ ์„ ๋‚ด๊ธฐ ์œ„ํ•˜์—ฌ ํ˜„์ˆ˜๋Š” ์„ ์ƒ๋‹˜์ด ์ฃผ์‹  N๊ฐœ์˜ ๋ฌธ์ œ๋ฅผ ํ’€๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ๋ฌธ์ œ๋Š” ๊ทธ๊ฒƒ์„ ํ’€์—ˆ์„ ๋•Œ ์–ป๋Š” ์ ์ˆ˜์™€ ํ‘ธ๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ์ฃผ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‹œ๊ฐ„ M์•ˆ์— N๊ฐœ์˜ ๋ฌธ์ œ ์ค‘ ์ตœ๋Œ€์ ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

(ํ•ด๋‹น๋ฌธ์ œ๋Š” ํ•ด๋‹น์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋ฉด ํ‘ธ๋Š” ๊ฑธ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค, ํ•œ ์œ ํ˜•๋‹น ํ•œ๊ฐœ๋งŒ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜N(1<=N<=20)๊ณผ ์ œํ•œ ์‹œ๊ฐ„ M(10<=M<=300)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N์ค„์— ๊ฑธ์ณ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์„ ๋•Œ์˜ ์ ์ˆ˜์™€ ํ‘ธ๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ œํ•œ ์‹œ๊ฐ„์•ˆ์— ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

5 20
10 5
25 12
15 8
6 3
7 4

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

41

 

 

๋ฌธ์ œ ํ’€์ด 1

public class GetMaximumScore
{
   static int n, m, max;
   static int[] score, times;
   
   public void DFS(int L, int time, int sum)
   {
      if (time > m) {
         return;
      }
      if (L == n) {
         if (sum > max) max = sum;
      }else {
         DFS(L + 1, time + times[L], sum + score[L]);
         DFS(L + 1, time, sum);
      }
   }
   
   public static void main(String[] args)
   {
      GetMaximumScore getMaximumScore = new GetMaximumScore();
      Scanner scanner = new Scanner(System.in);
      n = scanner.nextInt();
      m = scanner.nextInt();
      score = new int[n];
      times = new int[n];
      
      for (int i = 0; i < n; i++)
      {
         score[i] = scanner.nextInt();
         times[i] = scanner.nextInt();
      }
      
      getMaximumScore.DFS(0, 0, 0);
      System.out.println(max);
      
   }
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ์ด ๋ฌธ์ œ๋Š” ํ•ฉ์ด ๊ฐ™์€ ๋ถ€๋ถ„์ง‘ํ•ฉ, ๋ฐ”๋‘‘์ด ์Šน์ฐจ ๊ณผ ๊ฐ™์ด ๋ถ€๋ถ„ ์ง‘ํ•ฉ ํ’€์ด ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด์ฃผ๋ฉด ๋œ๋‹ค. (๋ฌธ์ œ ํ’€์ด ๋ฐฉ์‹์ด ์™„์ „ ๊ฐ™์Œ) 

๋Œ€์‹  ์ด ๋ฌธ์ œ์—์„œ๋Š” ์ ์ˆ˜์™€ ์‹œ๊ฐ„์ด ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง„ ์ตœ๋Œ€ ์‹œ๊ฐ„์„ ๋„˜๊ธฐ์ง€ ์•Š์œผ๋ฉด์„œ ์ตœ๋Œ€ ํ•ฉ์„ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. 

 

๋”ฐ๋ผ์„œ score, time ๊ฐ๊ฐ 1์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ๊ทธ ๋‹ค์Œ์—๋Š” ๋น„์Šทํ•˜๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ๋œ๋‹ค. 

๋ฌธ์ œ๋ฅผ ํ’€๊ณ , ์•ˆํ’€๊ณ ์— ๋”ฐ๋ผ์„œ ๋ฌธ์ œ๋ฅผ ํ‘ผ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด sum์— score[L]์„ ๋”ํ•˜๋ฉด์„œ time์— times[L]์„ ๋”ํ•ด์ฃผ๊ฑฐ๋‚˜, 

๋ฌธ์ œ๋ฅผ ํ’€์ง€ ์•Š์„ ๋•Œ๋Š” ๊ทธ๋ƒฅ sum๊ณผ time ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค. 

 

L์ด n๊นŒ์ง€ ๊ฐ”์„ ๋•Œ๋Š” max๊ฐ€ sum๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ sum์˜ ๊ฐ’์„ max์— ๋Œ€์ž…ํ•ด์ฃผ๊ณ  ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค. 

๋˜๋Š” time์ด m๋ณด๋‹ค ์ปค์งˆ ๊ฒฝ์šฐ์—๋Š” ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์•ˆ์— ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— return ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

๋ฌธ์ œ ํ’€์ด 2

public class GetMaximumScore2 {
    static int answer = Integer.MIN_VALUE, n, m;
    boolean flag = false;

    // ps : score, pt : time
    public 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) {
        GetMaximumScore2 getMaximumScore2 = new GetMaximumScore2();
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];

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

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

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜๋„ ๋น„์Šทํ•˜๊ฒŒ ํ’€์…จ๋Š”๋ฐ, ๊ฐ•์‚ฌ๋‹˜์€ DFS ๋ฉ”์„œ๋“œ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ ์ˆ˜์™€ ์‹œ๊ฐ„ ๋ฐฐ์—ด๋„ ํ•จ๊ป˜ ๋„˜๊ฒจ์ฃผ๊ณ  ์žˆ๋‹ค. 

ps๋Š” ์ ์ˆ˜ ๋ฐฐ์—ด์„ ์˜๋ฏธํ•˜๊ณ , pt๋Š” ์‹œ๊ฐ„ ๋ฐฐ์—ด์„ ์˜๋ฏธํ•œ๋‹ค. 

 

๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๋Š”

DFS(L + 1, sum + ps[L], time + pt[L], ps, pt); // ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ

์ด ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด๊ณ , 

 

๋ฌธ์ œ๋ฅผ ํ’€์ง€ ์•Š์„ ๋•Œ๋Š” 

DFS(L + 1, sum, time, ps, pt); // ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ์•Š์„ ๋•Œ

์ด ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฐ๊ฐ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ„๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  time์ด ์ œํ•œ๋œ ์‹œ๊ฐ„ m ์„ ๋„˜๊ธธ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ m๋ณด๋‹ค ์ปค์ง€๋ฉด return ํ•ด์ค˜ ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•˜๊ณ , 

L์ด n๊นŒ์ง€ ๋‹จ๊ณ„๊ฐ€ ๋‚ด๋ ค๊ฐ”์„ ๋•Œ๋Š” answer ์™€ sum ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ํฐ ๊ฐ’์„ answer์— ๋‹ด์•„์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

 

 

728x90