-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1697.java
More file actions
49 lines (44 loc) · 1.2 KB
/
P1697.java
File metadata and controls
49 lines (44 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
package BOJ;
import java.io.*;
import java.util.Queue;
import java.util.Arrays;
import java.util.LinkedList;
public class P1697 {
static int[] check = new int[100001];
public static int bfs(int start, int last){
Arrays.fill(check, -1);
Queue<Integer> q = new LinkedList<Integer>();
check[start] = 0;
q.add(start);
int position = 0;
if(last<start)
return start-last;
while(!q.isEmpty()){
position = q.remove();
if(position==last){
return check[position];
}
if(position-1 >=0 && check[position-1]==-1){
q.add(position-1);
check[position-1] = check[position]+1;
}
if(position+1 < 100001 && check[position+1]==-1){
q.add(position+1);
check[position+1] = check[position]+1;
}
if(position*2 < 100001 && check[position*2]==-1){
q.add(position*2);
check[position*2] = check[position]+1;
}
}
return 0;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] line = br.readLine().split(" ");
int start = Integer.parseInt(line[0]);
int last = Integer.parseInt(line[1]);
System.out.println(bfs(start, last));
}
}