728x90
import java.util.*;
public class Main {
static int[] visit= new int[100001];
static int start;
static int end;
public static void bfs(int v) {
Queue<Integer> q = new LinkedList<>();
q.add(v);
visit[v] = 1;
while (!q.isEmpty()) {
int temp = q.poll();
for (int i = 0; i < 3; i++) {
int next;
if (i == 0) {
next = temp + 1;
} else if (i == 1) {
next = temp - 1;
} else {
next = temp * 2;
}
if (next == end) {
System.out.println(visit[temp]);
return;
}
if (next >= 0 && next < visit.length && visit[next] == 0) {
q.add(next);
visit[next] = visit[temp] + 1;
}
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
start = scan.nextInt();//수빈
end = scan.nextInt();//동생
if(start == end)
System.out.println(0);
else
bfs(start);
scan.close();
}
}
728x90
'CS > Algorithm' 카테고리의 다른 글
[백준 1759] 소수의 연속합 - JAVA (0) | 2021.05.10 |
---|---|
[백준 10830] 행렬제곱 - java (0) | 2021.05.09 |
[백준:2606] 바이러스 - JAVA (0) | 2021.05.02 |
[백준:2178] 미로탐색 - JAVA (0) | 2021.05.02 |
[백준:2667] 단지번호 붙이기 - JAVA (0) | 2021.05.02 |