코딩테스트/[백준] 코딩테스트 연습
A -> B - 16953번
쵼쥬
2021. 11. 2. 13:51
내 코드
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;
}
}