-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj_16953.java
More file actions
51 lines (42 loc) · 1.2 KB
/
boj_16953.java
File metadata and controls
51 lines (42 loc) · 1.2 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
package greedy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class boj_16953 {
static long A,B;
static int count=0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
A = Long.parseLong(st.nextToken());
B = Long.parseLong(st.nextToken());
int res=solve();
System.out.println(res);
br.close();
}
private static int solve(){
Queue<Long> q = new LinkedList<>();
q.add(A);
while(!q.isEmpty()){
int size=q.size();
for(int i=0;i<size;i++){
long tmp=q.poll();
if(tmp==B){
count++;
return count;
}
if(tmp*2<=B){
q.add(tmp*2);
}
if(tmp*10+1<=B){
q.add(tmp*10+1);
}
}
count++;
}
return -1;
}
}