CS/Algorithm

[백준 : 13549/JAVA] 숨바꼭질3

yujindonut 2021. 9. 7. 15:40
728x90

import java.util.*;

class Main {
    private static int N; //수빈이 위치
    private static int K; //동생 위치
    private static boolean[] isVisited = new boolean[100001];
    private static int answer = Integer.MAX_VALUE;


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        K = sc.nextInt();
        bfs();
        System.out.println(answer);
        sc.close();
    }

    static void bfs() {
        PriorityQueue<Point> queue = new PriorityQueue<>();
        queue.offer(new Point(N, 0));
        while (!queue.isEmpty()) {
            Point point = queue.poll();
            
            if(isVisited[point.pos]) continue;
            isVisited[point.pos] = true; 
            
            if (point.pos == K) {
                answer = Math.min(point.count, answer);
            }
            if (point.pos * 2 <= 100000 && !isVisited[point.pos * 2]) { // 0초 순간이동
                queue.offer(new Point(point.pos * 2, point.count));
            }
            if (point.pos + 1 <= 100000 && !isVisited[point.pos + 1]) { //1초 한칸우측
                queue.offer(new Point(point.pos + 1, point.count + 1));
            }
            if (point.pos - 1 >= 0 && !isVisited[point.pos - 1]) { //1초 한칸좌측
                queue.offer(new Point(point.pos - 1, point.count + 1));
            }
        }
    }

    private static class Point implements Comparable<Point>{
        int pos;
        int count;

        public Point(int pos, int count) {
            this.pos = pos;
            this.count = count;
        }

        @Override
        public int compareTo(Point o) {
            return count - o.count;
        }
    }

}

bfs로 풀리는 문제였다.. 다른사람들 풀이 참고

728x90