Notice
Recent Posts
Recent Comments
Link
개발자-H 입니다.
BOJ - A -> B 본문
https://www.acmicpc.net/problem/16953
- 그래프 - 너비우선 탐색 문제이다.
- int 형으로 하다가 뒤에 1더하는 조건때문에 고생했는데.. long 으로 바꾸고 10* +1로 계산했다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
StringTokenizer st = new StringTokenizer(br.readLine());
long A = Integer.parseInt(st.nextToken());
long B = Integer.parseInt(st.nextToken());
if(A == B) {
System.out.println("-1");
return;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(new Node(A, 1));
while (!queue.isEmpty()) {
Node currentNode = queue.poll();
if (currentNode.index == B) {
System.out.println(currentNode.depth);
return;
}
if (currentNode.index > B) continue;
queue.offer(new Node(currentNode.index * 2, currentNode.depth + 1));
queue.offer(new Node(currentNode.index * 10 + 1, currentNode.depth + 1));
}
System.out.println("-1");
}
static class Node {
public long index;
public long depth;
public Node(long index, long depth) {
this.index = index;
this.depth = depth;
}
}
}
'Algorithm > 문제 풀이' 카테고리의 다른 글
BOJ - 경로 찾기 (0) | 2021.09.01 |
---|---|
BOJ - 듣보잡 (0) | 2021.08.31 |
BOJ - 트리의 부모 찾기 (0) | 2021.08.30 |
BOJ - 부녀회장이 될테야 (0) | 2021.08.29 |
BOJ - 좌표 정렬하기 (0) | 2021.08.28 |
Comments