A->B (16953)

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

๋ฌธ์ œ

์ •์ˆ˜ A๋ฅผ B๋กœ ๋ฐ”๊พธ๋ ค๊ณ  ํ•œ๋‹ค. ๊ฐ€๋Šฅํ•œ ์—ฐ์‚ฐ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€์ด๋‹ค.

  • 2๋ฅผ ๊ณฑํ•œ๋‹ค.

  • 1์„ ์ˆ˜์˜ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

A๋ฅผ B๋กœ ๋ฐ”๊พธ๋Š”๋ฐ ํ•„์š”ํ•œ ์—ฐ์‚ฐ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— A, B (1 โ‰ค A < B โ‰ค 109)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

A๋ฅผ B๋กœ ๋ฐ”๊พธ๋Š”๋ฐ ํ•„์š”ํ•œ ์—ฐ์‚ฐ์˜ ์ตœ์†Ÿ๊ฐ’์— 1์„ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

2 162

์˜ˆ์ œ ์ถœ๋ ฅ 1

5

์˜ˆ์ œ ์ž…๋ ฅ 2

4 42

์˜ˆ์ œ ์ถœ๋ ฅ 2

-1

์˜ˆ์ œ ์ž…๋ ฅ 3

100 40021

์˜ˆ์ œ ์ถœ๋ ฅ 3

5

๋ฌธ์ œ์˜ ๊ตฌํ˜„์€ ์–ด๋ ต์ง€ ์•Š์•˜์ง€๋งŒ ์—ญ์‹œ ๋ง‰ํ˜”๋˜ ๋ถ€๋ถ„์€ ํƒ€์ž…์—์„œ ๋ง‰ํ˜”๋‹ค.

10์˜ 9์Šน์„ ์ค€๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์ €์ •๋„์˜ ์ˆ˜๊ฐ€ ๋“ค์–ด์™€์„œ ๊ฒฐ๊ตญ ๋กœ์ง์„ ํƒ€๊ฒŒ ๋œ๋‹ค๋ฉด ์ˆซ๊ฐ€ ์—„์ฒญ ๋ถˆ์–ด๋‚  ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ๊ธฐ๋ณธ์ ์œผ๋กœ int ์˜ ํฌ๊ธฐ๊ฐ€ -20์–ต๋ถ€ํ„ฐ 20์–ต๊นŒ์ง€๋ผ๊ณ  ์•Œ๊ณ  ์žˆ๋‹ค โ†’ 9์ž๋ฆฌ์ˆ˜๊ฐ€ 1์–ต์ธ๋ฐ int์˜ ๋ฒ”์œ„๋Š” 10์ž๋ฆฌ์ˆ˜

๊ทธ๋Ÿผ 20์–ต์ด ๋“ค์–ด์™€์„œ ๊ฐ’์„ ๋Œ๊ฒŒ๋˜๋ฉด int์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— Long ํƒ€์ž…์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด์•ผ ํ–ˆ๋‹ค!!

DFS

import java.util.*;
import java.io.*;
public class Main{
    static long start, end;
    static int result = -1;
    public static void main(String[] args)throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        start = Integer.parseInt(st.nextToken());
        end = Integer.parseInt(st.nextToken());
        fn(start, 0);
        System.out.println(result);
    }

    public static void fn(long num, int count){
        count++;
        if(num == end){
            result = count;
            return;
        }

        if(num*2<=end){
            fn(num*2, count);
        }
        if(num*10+1<=end){
            fn(num*10+1, count);
        }

    }
}

BFS

import java.util.*;
import java.io.*;
public class Main{
    static long result;
    public static void main(String[] args)throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        long start = Long.parseLong(st.nextToken());
        long end = Long.parseLong(st.nextToken());
        fn(start, end, 0);
        System.out.println(result);
    }

    public static void fn(long start, long end, long count){
        Queue<Long[]> q = new LinkedList<>();
        q.add(new Long[]{start, count+1});
        while(!q.isEmpty()){
            Long[] tmp = q.poll();
            long num = tmp[0];
            long cnt = tmp[1];
            if(num == end){
                result = cnt;
                return;
            }
            if(num*2<=end){
                q.add(new Long[]{num*2, cnt+1});
            }
            if(num*10+1<=end){
                q.add(new Long[]{num*10+1, cnt+1});
            }
        }
        result = -1;
    }
}

Last updated

Was this helpful?