-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13549.java
More file actions
52 lines (44 loc) · 1.47 KB
/
13549.java
File metadata and controls
52 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* 백준 13549번 숨바꼭질 3
* bfs 알고리즘
*/
import java.util.*;
import java.util.List;
import java.awt.*;
import java.io.*;
public class Main {
static Boolean[] visited = new Boolean[200002];
static int[] dist = new int[200002];
static void bfs(int n, int k){
Deque<Integer> deque = new ArrayDeque<>();
deque.add(n);
visited[n]=true;
dist[n] = 0;
while(!deque.isEmpty()){
int now = deque.pollFirst();
if(n==k) continue;
if(now*2<=100000 && (!visited[now*2]||dist[now*2]>dist[now])){
deque.add(now*2);
visited[now*2]=true;
dist[now*2]=dist[now];
}
int operation[] = {now+1,now-1};
for(int next : operation){
if(0<=next && next<=100000 && (!visited[next]||dist[next]>dist[now]+1)){
deque.add(next);
visited[next]=true;
dist[next] = dist[now] + 1;
}
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
Arrays.fill(visited,false);
bfs(n,k);
System.out.println(dist[k]);
}
}