-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_13549.java
More file actions
66 lines (57 loc) · 1.74 KB
/
boj_13549.java
File metadata and controls
66 lines (57 loc) · 1.74 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class boj_13549 {
static int N,K;
// static int min=Integer.MAX_VALUE;
static boolean[] visit=new boolean[100001];
static int[] count=new int[100001];
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
N=Integer.parseInt(st.nextToken());
K=Integer.parseInt(st.nextToken());
Arrays.fill(count,100001);
if(N==K){
System.out.println(0);
}else{
bfs(N);
}
}
private static void bfs(int start){
Queue<Integer> q=new LinkedList<>();
q.add(start);
count[start]=0;
while(!q.isEmpty()){
int cur=q.poll();
visit[cur]=true;
if(cur==K){
System.out.println(count[cur]);
return;
}
for(int i=0;i<3;i++){
int nxt;
if(i==0){
nxt=cur*2;
}else if(i==1){
nxt=cur+1;
}else{
nxt=cur-1;
}
if(nxt>=0 && nxt<visit.length && !visit[nxt]){
q.add(nxt);
if(nxt!=cur*2){
count[nxt]=Math.min(count[cur]+1,count[nxt]);
}else{
count[nxt]=Math.min(count[cur],count[nxt]);
}
}
}
}
}
}