CS/Algorithm
[백준:1697] 숨바꼭질 - JAVA
yujindonut
2021. 5. 3. 00:53
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