1. ๋ฌธ์ œ

Problem https://www.acmicpc.net/problem/1463

  • ๋ฌธ์ œ ์„ค๋ช…: n๊ฐ’์ด ์ฃผ์–ด์ง€๋ฉด 3๊ฐœ์˜ ๊ณผ์ •์ค‘ ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ 1์— ๋„๋‹ฌํ•˜๊ธฐ๊นŒ์ง€ ์ตœ์†Œ ๊ณ„์‚ฐ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.
    ์„ ํƒ ๊ฐ€๋Šฅํ•œ ๊ณผ์ •์ด 2๊ฐœ์˜€๋‹ค๋ฉด ์ด์ง„ ํŠธ๋ฆฌ ํƒ์ƒ‰์ด๋‚˜ ๊ณ ๋“ฑํ•™๊ต ํ™•๋ฅ ๊ณผ ํ†ต๊ณ„ ๊ณผ๋ชฉ์—์„œ ๋ฐฐ์šฐ๋Š” โ€˜๊ฐ™์€ ๊ฒƒ์ด ์žˆ๋Š” ์ˆœ์—ดโ€™ ๊ฐœ๋…์„ ํ™œ์šฉํ•˜์—ฌ ์ˆ˜ํ•™์ ์œผ๋กœ ํ’€์ˆ˜ ์žˆ๋‹ค.
    ๊ทธ๋Ÿฌ๋‚˜ ์ด๋ฌธ์ œ๋Š” ๊ณผ์ •์ด 3๊ฐœ์ด๋ฏ€๋กœ ์ด์ง„ํŠธ๋ฆฌ๋Š” ํ•ด๋‹น์‚ฌํ•ญ์ด ์—†๊ณ , ์ˆ˜ํ•™์ ์œผ๋กœ ํ’€๊ธฐ์—๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋งค์šฐ ๋ณต์žกํ•ด์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค.
    ๋”ฐ๋ผ์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊นŠ์ด ์šฐ์„ ์œผ๋กœ ํŠธ๋ฆฌ๋ฅผ ํƒ์ƒ‰(DFS)ํ•˜๋„๋ก ํ•˜๊ณ  ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ(DP)๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ตœ์ ํ™”๋ฅผ ์ง„ํ–‰ํ•˜์˜€๋‹ค.
  • ์‚ฌ์šฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : DP, DFS

2. ํ’€์ด

2.1. ์ดˆ๊ธฐ ์ฝ”๋“œ

  • ์ฝ”๋“œ:

    // ์ตœ์ ํ™” ๋˜์ง€์•Š์€ DFS (์ „์ˆ˜์กฐ์‚ฌ)
    import java.io.*;
    
    public class Main
    {
        static int minDepth = 1000000;
        static void search(int n,int depth) {
            if (n == 1) {
                minDepth = Math.min(depth, minDepth);
                return;
            }
            for(int i=0; i<2; i+=1)
                if(n%(3-i) == 0) search(n/(3-i), depth+1);
            search(n-1,depth+1);
        }
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            int n = Integer.parseInt(br.readLine());
            search(n,0);
            bw.write(Integer.toString(minDepth));
            bw.flush();
            bw.close();
        }
    }
    
  • ๋ฌธ์ œ์ :
    • ์˜ค์ง DFS ๋งŒ์œผ๋กœ 1์„ ๋งŒ๋“ค์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์ „์ˆ˜ ์กฐ์‚ฌ ํ•จ์œผ๋กœ์จ n๊ฐ’์ด ์ปค์งˆ์ˆ˜๋ก ์‹คํ–‰์‹œ๊ฐ„์ด ๊ธฐํ•˜ ๊ธ‰์ˆ˜์ ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค.
  • ๊ฒฐ๊ณผ : ์‹œ๊ฐ„ ์ดˆ๊ณผ

2.2. ์ตœ์ ํ™” ๊ณผ์ •

  • ์ตœ์ ํ™” ํ•„์š”์„ฑ: ํŠธ๋ฆฌ ํƒ์ƒ‰ ๊ด€์ ์œผ๋กœ ๋ดค์„๋•Œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ „์ˆ˜์กฐ์‚ฌํ•จ์œผ๋กœ ๋งค์šฐ ์˜ค๋žœ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ฒŒ ๋˜๊ณ  ๋น„ํšจ์œจ์ ์ด๋‹ค.
  • ์ตœ์ ํ™” ์ „๋žต: ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด ๋ถˆํ•„์š”ํ•œ ๋…ธ๋“œ ํƒ์ƒ‰ ๊ณผ์ • ์ œ๊ฑฐ
    • ๋ณ€๊ฒฝ: ๋…ธ๋“œ ํƒ์ƒ‰ ์ค‘ ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ์†Œ ์ž‘์—… ํšŸ์ˆ˜๋ณด๋‹ค ๋” ๊นŠ์€ ๋…ธ๋“œ์˜ ํƒ์ƒ‰์‹œ ํƒ์ƒ‰ ์ž‘์—…์„ ์ฆ‰์‹œ ์ข…๋ฃŒ

2.3. ์ตœ์ ํ™”๋œ ์ฝ”๋“œ (1์ฐจ)

  • ์ฝ”๋“œ:

    // DFS ์ตœ์ ํ™” (1์ฐจ)
    import java.io.*;
    
    public class Main
    {
        static int minDepth = 1000000;
        static void search(int n,int depth) {
            if (n == 1) {
                minDepth = Math.min(depth, minDepth);
                return;
            }
            //์ถ”๊ฐ€๋˜์–ด์ง„ ์กฐ๊ฑด
            if (minDepth == depth) return;
            for(int i=0; i<2; i+=1)
                if(n%(3-i) == 0) search(n/(3-i), depth+1);
            search(n-1,depth+1);
        }
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            int n = Integer.parseInt(br.readLine());
            search(n,0);
            bw.write(Integer.toString(minDepth));
            bw.flush();
            bw.close();
        }
    }
    
  • ์ฝ”๋“œ ์„ค๋ช…:

    • ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋˜, ํƒ์ƒ‰ํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ๊นŠ์ด(Depth)๊ฐ€ ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ตฌํ•œ 1์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ๊นŠ์ด๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๊ฒฝ์šฐ ๊ณผ์ •์„ ์ค‘๋‹จํ•˜๋„๋ก ํ•˜์—ฌ ์‹คํ–‰ ์‹œ๊ฐ„์„ ๋‹จ์ถ• ์‹œ์ผฐ์Œ.

2.4. ๊ฒฐ๊ณผ

  • ๊ฒฐ๊ณผ ์„ค๋ช…: ๊ธฐ์กด ์ฝ”๋“œ์— ๋น„ํ•ด ํƒ์ƒ‰ํ•˜๋Š” ๋…ธ๋“œ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋Œ€ํญ ๊ฐ์†Œํ•˜์˜€๋‹ค.
  • ๋น„๊ต ๋ถ„์„ (n=1234):

    • ํƒ์ƒ‰ ๋…ธ๋“œ ๊ฐฏ์ˆ˜
      • ์ดˆ๊ธฐ ์ฝ”๋“œ : ๊ฒฐ๊ณผ ๋„์ถœ ๋ถˆ๊ฐ€ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
      • ์ตœ์ ํ™”(1์ฐจ) : 1970๊ฐœ
    • ์‹คํ–‰ ์†๋„(์ œ์ถœ ๊ฒฐ๊ณผ)
      • ์ดˆ๊ธฐ ์ฝ”๋“œ : ๊ฒฐ๊ณผ ๋„์ถœ ๋ถˆ๊ฐ€ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
      • ์ตœ์ ํ™”(1์ฐจ) : 140 ms
  • ๋ฌธ์ œ์ :
    • ์ดˆ๊ธฐ ์ฝ”๋“œ์— ๋น„ํ•ด ํƒ์ƒ‰ ๋…ธ๋“œ ๊ฐฏ์ˆ˜๋Š” ์ค„์—ˆ์œผ๋‚˜ ์—ฌ์ „ํžˆ ํšŸ์ˆ˜๊ฐ€ ์ž‘์ง„ ์•Š๋‹ค.
  • ๊ฒฐ๊ณผ : ์ •๋‹ต (14340KB, 140ms)

2.5. ์ตœ์ ํ™” ๊ณผ์ •

  • ์ตœ์ ํ™” ํ•„์š”์„ฑ: ์ด๋ฏธ ์ตœ์†Œ ๊ณ„์‚ฐ ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ์ด๋ ฅ์ด ์žˆ๋Š” ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ๋„ ์ค‘๋ณต์œผ๋กœ ๊ณ„์‚ฐ์„ ํ•จ์œผ๋กœ์จ ํƒ์ƒ‰ ๋…ธ๋“œ ๊ฐฏ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚จ.
  • ์ตœ์ ํ™” ์ „๋žต: ๊ณ„์‚ฐํ–ˆ๋˜ ๊ฐ’๋“ค์„ ๋‹ค์‹œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์ • (DP)
    • ๋ณ€๊ฒฝ: ๊ณ„์‚ฐํ•œ ์ตœ์†Œ ๊ณ„์‚ฐ ํšŸ์ˆ˜๋ฅผ ๋ณ„๋„๋กœ ๊ธฐ๋กํ•˜๊ณ , ๋™์ผํ•œ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ์˜ ์ตœ์†Œ ๊ณ„์‚ฐ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ• ๋•Œ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ  ์ด๋ฏธ ๊ตฌํ•œ ๊ฐ’์„ ํ™œ์šฉํ•˜๋„๋ก ์ˆ˜์ •

2.6. ์ตœ์ ํ™”๋œ ์ฝ”๋“œ(2์ฐจ)

  • ์ฝ”๋“œ:

    // DFS ์ตœ์ ํ™” (2์ฐจ)
    import java.io.*;
    
    public class Main
    {
        static int minDepth = 1000000;
        static int[] depthDict;
        static int search(int n,int depth) {
            if (n == 1) {
                minDepth = Math.min(depth, minDepth);
                return 1;
            }
            if(minDepth == depth) return 0;
            if (depthDict[n-1] != 0) {
                minDepth = Math.min(depth+depthDict[n-1], minDepth);
                return depthDict[n-1]+1;
            }
            int partialMinDepth = 1000000;
            for(int i=0; i<2; i+=1)
                if(n%(3-i) == 0) {
                    int tmp = search(n/(3-i), depth+1);
                    if(tmp == 0) continue;
                    partialMinDepth = Math.min(tmp,partialMinDepth);
                }
            int tmp = search(n-1,depth+1);
            if (tmp != 0) partialMinDepth = Math.min(tmp,partialMinDepth);
            if (partialMinDepth != 1000000) depthDict[n-1] = partialMinDepth;
            return partialMinDepth+1;
        }
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            int n = Integer.parseInt(br.readLine());
            depthDict = new int[n];
            search(n,0);
            bw.write(Integer.toString(minDepth));
            bw.flush();
            bw.close();
        }
    }
    
  • ์ฝ”๋“œ ์„ค๋ช…:

    • 1์ฐจ ์ฝ”๋“œ์— ์ถ”๊ฐ€๋กœ DP ๊ฐœ๋…์„ ์ ์šฉ. ๊ฐ ๋…ธ๋“œ๋กœ ๋ถ€ํ„ฐ 1๊นŒ์ง€์˜ ์ตœ์†Œ ๊ณผ์ •ํšŸ์ˆ˜๋ฅผ ๋ณ„๋„๋กœ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ , ๋™์ผํ•œ ์ˆ˜ ๋…ธ๋“œ ๊ฒ€์‚ฌ์‹œ ์ €์žฅํ•œ ์ตœ์†Œ ํšŸ์ˆ˜๊ฐ’์„ ๋ถˆ๋Ÿฌ์™€ ์ค‘๋ณต๊ณ„์‚ฐ์„ ๋ฐฉ์ง€ํ•˜์˜€๋‹ค.

2.7. ๊ฒฐ๊ณผ

  • ๊ฒฐ๊ณผ ์„ค๋ช…: ์ตœ์ ํ™” ์ฝ”๋“œ(1์ฐจ)์— ๋น„ํ•ด ํƒ์ƒ‰ํ•˜๋Š” ๋…ธ๋“œ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋Œ€ํญ ๊ฐ์†Œํ•˜์˜€๋‹ค.
  • ๋น„๊ต ๋ถ„์„ (n=1234):
    • ํƒ์ƒ‰ ๋…ธ๋“œ ๊ฐฏ์ˆ˜
      • ์ตœ์ ํ™”(1์ฐจ) : 1970๊ฐœ
      • ์ตœ์ ํ™”(2์ฐจ) : 11๊ฐœ
    • ์‹คํ–‰ ์†๋„(์ œ์ถœ ๊ฒฐ๊ณผ)
      • ์ตœ์ ํ™”(1์ฐจ) : 140 ms
      • ์ตœ์ ํ™”(2์ฐจ) : 104 ms
  • ๊ฒฐ๊ณผ: ์ •๋‹ต (18132KB, 104ms)

3. ๊ฒฐ๋ก 

  • ์š”์•ฝ: ํŠธ๋ฆฌ ํƒ์ƒ‰์„ ์ด์šฉํ• ๋•Œ ๋…ธ๋“œ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋งŽ์€๊ฒฝ์šฐ ์‹คํ–‰ ์‹œ๊ฐ„์ด ๋งค์šฐ ๊ธธ์–ด์งˆ์ˆ˜ ์žˆ๋‹ค. ๋ณ„๋„์˜ ์กฐ๊ฑด์„ ๋ถ€์—ฌํ•˜๊ฑฐ๋‚˜ DP๊ฐ™์€ ์ถ”๊ฐ€์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐ™์ด ํ™œ์šฉํ•˜์—ฌ ๋…ธ๋“œ ํƒ์ƒ‰ํšŸ์ˆ˜๋ฅผ ์ค„์ด๋ฉด์„œ ์‹คํ–‰ ์‹œ๊ฐ„์„ ๋‹จ์ถ• ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

4. ์ฐธ๊ณ  ์ž๋ฃŒ

  • DFS ๊ฐœ๋… : <๊ฒŒ์‹œ๊ธ€ ์ค€๋น„ ์ค‘>
  • DP ๊ฐœ๋… : <๊ฒŒ์‹œ๊ธ€ ์ค€๋น„ ์ค‘>