숨바꼭질1697

LJM·2023년 10월 4일
0

백준풀기

목록 보기
253/259

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

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

public class Main {
    public static void main(String[] args) throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        boolean[] visit = new boolean[100001];
        Queue<int[]> que = new LinkedList<>();

        que.add(new int[]{N, 0});
        visit[N] = true;

        int answer = 0;
        while(que.isEmpty()==false){

            int[] cur = que.poll();

            if(cur[0] == K){
                answer = cur[1];
                break;
            }

            int np = cur[0]-1;
            if(np >= 0 && np <= 100000 && visit[np]==false){
                visit[np] = true;
                que.add(new int[]{np, cur[1]+1});
            }

            np = cur[0]+1;
            if(np >= 0 && np <= 100000 && visit[np]==false){
                visit[np] = true;
                que.add(new int[]{np, cur[1]+1});
            }

            np = cur[0]*2;
            if(np >= 0 && np <= 100000 && visit[np]==false){
                visit[np] = true;
                que.add(new int[]{np, cur[1]+1});
            }
        }

        System.out.println(answer);
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글