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

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

728x90

 

https://hyejin.tistory.com/1277

 

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

https://hyejin.tistory.com/1276 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch08. DFS, BFS ํ™œ์šฉ : ๋ฐ”๋‘‘์ด ์Šน์ฐจ DFS https://hyejin.tistory.com/1266 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch08. DFS, BFS ํ™œ์šฉ : ํ•ฉ์ด ๊ฐ™์€ ๋ถ€๋ถ„ ์ง‘ํ•ฉ (DFS, ์•„

hyejin.tistory.com

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

 

 

 

4. ์ค‘๋ณต์ˆœ์—ด ๊ตฌํ•˜๊ธฐ 

์„ค๋ช…

1๋ถ€ํ„ฐ N๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ์ ํžŒ ๊ตฌ์Šฌ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์ค‘ ์ค‘๋ณต์„ ํ—ˆ๋ฝํ•˜์—ฌ M๋ฒˆ์„ ๋ฝ‘์•„ ์ผ๋ ฌ๋กœ ๋‚˜์—ด ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์ž…๋ ฅ ์„ค๋ช…

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(3<=N<=10)๊ณผ M(2<=M<=N) ์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ ์„ค๋ช…

์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ถœ๋ ฅ์ˆœ์„œ๋Š” ์‚ฌ์ „์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์ž…๋ ฅ ์˜ˆ์ œ 1

3 2 

 

์ถœ๋ ฅ ์˜ˆ์ œ 1

1 1

1 2

1 3

2 1

2 2

2 3

3 1

3 2

3 3

 

 

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

public class FindingDuplicatePermutations
{
   static int n,m;
   
   public void DFS(int val)
   {
      if (val > n) {
         return;
      }else {
         for (int i = 1; i <= n; i++)
         {
            System.out.println(val + " " + i);
         }
         DFS(val + 1);
      }
   }
   
   public static void main(String[] args)
   {
      FindingDuplicatePermutations findingDuplicatePermutations = new FindingDuplicatePermutations();
      Scanner scanner = new Scanner(System.in);
      n = scanner.nextInt();
      m = scanner.nextInt();
      
      findingDuplicatePermutations.DFS(1);
   }
}

๐Ÿ‘ฉ‍๐Ÿ’ป -> ์”.. ใ…‹ใ…‹ ์ด๋ ‡๊ฒŒ ํ’€๋ฉด ์•ˆ๋œ๋‹ค,,, 2๋ฒˆ ๋ฝ‘์„๋•Œ๋Š” ์ถœ๋ ฅ ์˜ˆ์ œ์— ๋งž๊ฒŒ ์ถœ๋ ฅ๋˜๊ฒ ์ง€๋งŒ 3๋ฒˆ ๋ฝ‘์„ ๋•Œ๋ถ€ํ„ฐ๋Š” ์ถœ๋ ฅ ์˜ˆ์ œ์— ๋งž๊ฒŒ ์•ˆ๋œ๋‹ค..!!!! ๋ฐ”๋ณด ์•„๋‹Œ๊ฐ€ ์‹ถ๋„ค.. ใ…Ž 

 

 

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

static int[] pm;
static int n, m;

public void DFS(int L)
{
   if (L == m) {
      for (int i : pm)
      {
         System.out.print(i + " ");
      }
         System.out.println();
   }else {
      for (int i = 1; i <= n; i ++ )
      {
         pm[L] = i;
         DFS(L + 1);
      }
   }
}

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜ ํ’€์ด์ธ๋ฐ ์šฐ์„  pm ์ด๋ผ๋Š” ๋ฐฐ์—ด ํ•˜๋‚˜๋ฅผ ์„ ์–ธํ•ด์„œ ํ’€์–ด์•ผ ํ•œ๋‹ค. 

pm[L] = i; ์ด๋ ‡๊ฒŒ ์„ ์–ธ์„ ํ•ด์ฃผ๊ณ , DFS(L+1) ์„ ์žฌ๊ท€ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋‹ต์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค. 

์ฒ˜์Œ์—๋Š” ๋‚˜๋„ ์ดํ•ด๊ฐ€ ์•ˆ๋ผ์„œ ์ง์ ‘ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค์„œ ํ•ด๋ณด๋‹ˆ๊นŒ ์ดํ•ด๊ฐ€ ๋๋‹ค. 

 

๋ง๋กœ ํ’€์–ด๋ณด์ž๋ฉด... 

DFS(0) ์—์„œ ์‹œ์ž‘์„ ํ•˜๋Š”๋ฐ if ์—์„œ 0์ด m ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ else ๋ฌธ์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

else ๋ฌธ์—์„œ for๋ฌธ์„ ๋“ค์–ด์˜ค๋Š”๋ฐ i = 1 ์—์„œ pm[0] = 1; ์„ ๋„ฃ์–ด์ฃผ๊ณ , DFS(1) ์„ ํ˜ธ์ถœํ•œ๋‹ค. 

DFS(1) ์—์„œ else ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ ๋„˜์–ด์™€ i = 1์—์„œ pm[1]  = 1; ์„ ๋„ฃ์–ด์ฃผ๊ณ , DFS(2) ์„ ํ˜ธ์ถœํ•œ๋‹ค. 

DFS(2)๋Š” L == m์— ํ•ด๋‹นํ•˜๋ฏ€๋กœ pm[0], pm[1] ์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.  -> 1 1 

 

๋‹ค์‹œ DFS(1) ์˜ i = 2 ๋กœ ๋„˜์–ด์™€์„œ pm[1] = 2; ์„ ๋„ฃ์–ด์ฃผ๊ณ , ๋‹ค์‹œ DFS(2)์„ ํ˜ธ์ถœํ•œ๋‹ค. 

DFS(2)๋Š” L == m์— ํ•ด๋‹นํ•˜๋ฏ€๋กœ pm[0], pm[1] ์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค. -> 1 2 

 

DFS(1)์˜ i = 3์œผ๋กœ ๋„˜์–ด์™€์„œ pm[1] =3; ์„ ๋„ฃ์–ด์ฃผ๊ณ , DFS(2) ์„ ํ˜ธ์ถœํ•œ๋‹ค.

DFS(2)๋Š” L == m์— ํ•ด๋‹นํ•˜๋ฏ€๋กœ  pm[0], pm[1] ์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค. -> 1 3  

 

DFS(0), i=2 ๋กœ ๋„˜์–ด์™€์„œ pm[0] = 2 ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  DFS(1) ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

DFS(1) i = 1, pm[1] = 1 ๋„ฃ์–ด์ฃผ๊ณ  DFS(2) ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

DFS(2) ๋Š” L == m์— ํ•ด๋‹นํ•˜๋ฏ€๋กœ  pm[0], pm[1] ์„ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค. -> 2 1 

 

... ์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณตํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

 

 

 

 

 

 

728x90