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

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

728x90

 

https://hyejin.tistory.com/1266

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch08. DFS, BFS ํ™œ์šฉ : ํ•ฉ์ด ๊ฐ™์€ ๋ถ€๋ถ„ ์ง‘ํ•ฉ (DFS, ์•„๋งˆ์กด ์ธํ„ฐ๋ทฐ)

https://hyejin.tistory.com/1265 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree, Graph(DFS, BFS ๊ธฐ์ดˆ) : ๊ทธ๋ž˜ํ”„ ์ตœ๋‹จ๊ฑฐ๋ฆฌ (BFS) https://hyejin.tistory.com/1264 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch07. Recursive, Tree, Graph(DF

hyejin.tistory.com

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

 

 

 

2. ๋ฐ”๋‘‘์ด ์Šน์ฐจ 

์„ค๋ช…

์ฒ ์ˆ˜๋Š” ๊ทธ์˜ ๋ฐ”๋‘‘์ด๋“ค์„ ๋ฐ๋ฆฌ๊ณ  ์‹œ์žฅ์— ๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ทธ์˜ ํŠธ๋Ÿญ์€ Cํ‚ฌ๋กœ๊ทธ๋žจ ๋„˜๊ฒŒ ํƒœ์šธ์ˆ˜๊ฐ€ ์—†๋‹ค.

์ฒ ์ˆ˜๋Š” C๋ฅผ ๋„˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ทธ์˜ ๋ฐ”๋‘‘์ด๋“ค์„ ๊ฐ€์žฅ ๋ฌด๊ฒ๊ฒŒ ํƒœ์šฐ๊ณ  ์‹ถ๋‹ค.

N๋งˆ๋ฆฌ์˜ ๋ฐ”๋‘‘์ด์™€ ๊ฐ ๋ฐ”๋‘‘์ด์˜ ๋ฌด๊ฒŒ W๊ฐ€ ์ฃผ์–ด์ง€๋ฉด,

์ฒ ์ˆ˜๊ฐ€ ํŠธ๋Ÿญ์— ํƒœ์šธ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ๋ฌด๊ฒŒ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ C(1<=C<=100,000,000)์™€ N(1<=N<=30)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๋งˆ๋ฆฌ ๋ฐ”๋‘‘์ด์˜ ๋ฌด๊ฒŒ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ๋ฌด๊ฒŒ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

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

259 5
81
58
42
33
61

 

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

242

 

 

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

public class BadukiRiding
{
   static int c, n, answer = 0;
   static int[] arr;
   
   // ๋ถ€๋ถ„ ์ง‘ํ•ฉ ์ฝ”๋“œ์™€ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๊ฒŒ ํ’€๋ฉด ๋œ๋‹ค.
   public void DFS(int L, int sum )
   {
      if (sum > c) {
         return;
      }
      if (L == n) {
         if (answer < sum) answer = sum;
      }else {
         DFS(L + 1, sum + arr[L]);
         DFS(L + 1, sum);
      }
   }
   
   public static void main(String[] args)
   {
      BadukiRiding badukiRiding = new BadukiRiding();
      Scanner scanner = new Scanner(System.in);
       c = scanner.nextInt(); // ์ตœ๋Œ€ ๋ฌด๊ฒŒ
       n = scanner.nextInt();
      arr = new int[n];
      for (int i = 0; i< n; i++)
      {
         arr[i] = scanner.nextInt();
      }
      
      badukiRiding.DFS(0, 0);
      System.out.println(answer);
      
   }
}

๐Ÿ‘ฉ‍๐Ÿ’ป : ์ด ๋ฌธ์ œ๋Š” ์ด์ „ ๋ฌธ์ œ์—์„œ ํ’€์—ˆ๋˜ ํ•ฉ์ด ๊ฐ™์€ ๋ถ€๋ถ„ ์ง‘ํ•ฉ ๊ตฌํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ๋œ๋‹ค. (ํ’€์ด ๋ฐฉ์‹์ด ๊ฑฐ~~์˜ ๋น„์Šทํ•จ) 

 

static int c, n, answer = 0;
static int[] arr;

-> ๋จผ์ € static ๋ณ€์ˆ˜๋กœ c, n, answer๋ฅผ ๋ชจ๋‘ ์„ ์–ธํ•ด์ฃผ๋Š”๋ฐ c๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ n์€ ๋ฐ”๋‘‘์ด ๋งˆ๋ฆฌ ์ˆ˜, answer์— ๋ฐ”๋‘‘์ด๋“ค์„ ํƒœ์šธ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ๋ฅผ ๋‹ด์„ ๋ณ€์ˆ˜์ด๋‹ค. (arr์€ ๊ฐ ๋ฐ”๋‘‘์ด๋“ค ๋ฌด๊ฒŒ) 

 

public void DFS(int L, int sum )
{
   if (sum > c) {
      return;
   }
   if (L == n) {
      if (answer < sum) answer = sum;
   }else {
      DFS(L + 1, sum + arr[L]);
      DFS(L + 1, sum);
   }
}

-> DFS ๋ฉ”์„œ๋“œ์—๋Š” L (๋ ˆ๋ฒจ)๊ณผ sum์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์„ ๊ฒƒ์ด๋‹ค. 

๋งŒ์•ฝ sum์ด c๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด ๋” ์ด์ƒ ๋ฐ”๋‘‘์ด๋ฅผ ํƒœ์šธ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Œ€๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค. 

 

์•„๋‹ˆ๋ฉด L์ด ๋ฐ”๋‘‘์ด ๋งˆ๋ฆฌ์ˆ˜๋งŒํผ ๋”ํ–ˆ๋‹ค๋ฉด ์ด์ œ ์ด sum์ด answer ๋ณด๋‹ค ํฌ๋‹ค๋ฉด answer์— sum ๊ฐ’์„ ๋‹ด์•„์ค€๋‹ค. (c๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ๋Š” ์œ„์—์„œ ๊ฑธ๋Ÿฌ์คฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Œ€๋กœ ๋‹ด์•„๋„ ๋œ๋‹ค.) 

 

์ด ๊ฒฝ์šฐ๋„ ์•„๋‹ˆ๋ผ๋ฉด DFS๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ sum์— ํ•ด๋‹น ๋ฐ”๋‘‘์ด๋ฅผ ํƒœ์šด ํ•ฉ๊ณผ, ํƒœ์šฐ์ง€ ์•Š์€ ํ•ฉ์„ ๊ฐ๊ฐ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ˜ธ์ถœํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

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

static int answer = Integer.MIN_VALUE, c, n;

public void DFS(int L, int sum, int[] arr)
{
   if (sum > c) return;
   if (L == n) {
      answer = Math.max(sum, answer);
   }else {
      DFS(L+1, sum + arr[L], arr);
      DFS(L+1, sum, arr);
   }
}

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜์ด ํ’€์ดํ•œ ๋ฐฉ์‹๋„ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ธด ํ•˜๋‹ค. ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด๋ผ๋ฉด answer์— ๊ฐ’์„ ๊ตฌํ•  ๋•Œ Math.max ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด sum๊ณผ answer ์ค‘์— ํฐ ๊ฐ’์„ answer์— ๋‹ด์•„์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90