1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 1


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/1697

image


๐Ÿ“Œ ํ’€์ด

์ฒ˜์Œ ์ด ๋ฌธ์ œ๋ฅผ ๋”ฑ ๋ณด์ž ๋Š๋‚€ ์ƒ๊ฐ์€ BFS๋ž‘ ๊ทธ ์–ด๋–ค ์—ฐ๊ด€๋„ ์—†์–ด๋ณด์ด๋Š”๋ฐ, ์ฐจ๋ผ๋ฆฌ DP๊ด€๋ จ ๋ฌธ์ œ๊ฐ€ ์•„๋‹Œ๊ฐ€? ํ•˜๋Š” ์ƒ๊ฐ์ด์—ˆ๋‹ค.
์™œ๋ƒํ•˜๋ฉด ๋‚ด๊ฐ€ ์ง€๊ธˆ๊นŒ์ง€ ํ’€์–ด๋ณธ DP๋ฌธ์ œ์—์„œ ๋Š๋‚€ ์ ์€ ๋ฐ”๋กœ ์ ˆ๋Œ€๋กœ ๋ถ€๋ถ„์˜ ์ตœ์ ํ•ด๊ฐ€ ์ „์ฒด์˜ ์ตœ์ ํ•ด๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

๊ทผ๋ฐ ๊ฐ€๋งŒํžˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ DP์™€ BFS๋ฅผ ๋‘˜๋‹ค ์ด๋ฏธ ์ง€๋‚œ ๊ณณ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ทธ ์œ„์น˜์— ์ €์žฅํ•˜๊ณ , ๋‹ค์Œ์— ๊ทธ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋˜๊ฒŒ ๋น„์Šทํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ sujin๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ ๋ชจ๋“  ๋ฐฐ์—ด์— ์œ„์น˜์— ์ง€๊ธˆ๊นŒ์ง€ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ๋„ฃ์–ด์„œ ์ตœ์ข… sister์˜ ์œ„์น˜์— ๋„๋‹ฌํ•œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„์„ ํ•˜์˜€๋‹ค.

์ฒ˜์Œ BFS๊ด€๋ จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  ๋‚˜์„œ ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ์ ‘ํ•˜๋‹ˆ๊นŒ, ํ›จ์”ฌ ์‰ฝ๊ฒŒ ํ’€๋ฆฌ๋Š” ๋Š๋‚Œ์ด ๋“ค์—ˆ๋‹ค.


๐Ÿ“Œ Code

package BFS;

import java.io.*;
import java.util.*;

public class N1697 {
    static int arr[];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int subin = Integer.parseInt(st.nextToken());
        int sister = Integer.parseInt(st.nextToken());

        int max = 100000;
        arr = new int[max + 1];
        Arrays.fill(arr, -1);
        Queue<Integer> q = new LinkedList<>();

        arr[subin] = 0;
        q.add(subin);

        if ( subin == sister){
            System.out.println(0);
            return;
        }
        int cur;
        int nextPos;
        int time = 0;
        while (!q.isEmpty()) {
            cur = q.poll();
            nextPos = cur - 1;
            time = arr[cur] + 1;
            if (nextPos >= 0 && arr[nextPos] == -1) {
                arr[nextPos] = time;
                q.add(nextPos);
                if (nextPos == sister) {
                    System.out.println(time);
                    break;
                }
            }
            nextPos = cur + 1;
            if (nextPos <= max && arr[nextPos] == -1) {
                arr[nextPos] = time;
                q.add(nextPos);
                if (nextPos == sister) {
                    System.out.println(time);
                    break;
                }
            }
            nextPos = 2 * cur;
            if (nextPos <= max && arr[nextPos] == -1) {
                arr[nextPos] = time;
                q.add(nextPos);
                if (nextPos == sister) {
                    System.out.println(time);
                    break;
                }
            }
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜