내 코드
package com.company;
import java.io.*;
import java.util.*;
public class Main {
static int A, B;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
A = Integer.parseInt(st.nextToken());
B = Integer.parseInt(st.nextToken());
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(B, 1));
int answer = 0;
while (!pq.isEmpty()) {
Node node = pq.poll();
if (node.getValue() == A) {
answer = node.getCount();
break;
}
if (node.getValue() == 1)
continue;
if (node.getValue() % 10 == 1)
pq.add(new Node(node.getValue() / 10, node.getCount() + 1));
if (node.getValue() % 2 == 0)
pq.add(new Node(node.getValue() / 2, node.getCount() + 1));
}
System.out.println(answer == 0 ? -1 : answer);
}
}
class Node implements Comparable<Node> {
private int value, count;
public int getValue() {
return value;
}
public int getCount() {
return count;
}
public Node(int value, int count) {
this.value = value;
this.count = count;
}
@Override
public int compareTo(Node other) {
return this.count - other.count;
}
}
'코딩테스트 > [백준] 코딩테스트 연습' 카테고리의 다른 글
Coins - 3067번 (0) | 2021.11.04 |
---|---|
뱀 - 3190번 (0) | 2021.11.02 |
특정한 최단 경로 - 1504번 (0) | 2021.11.01 |
독서실 거리두기 - 20665번 (0) | 2021.10.27 |
뱀과 사디리 게임 - 16928번 (0) | 2021.10.27 |