-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ1697.java
More file actions
58 lines (45 loc) · 1.07 KB
/
BJ1697.java
File metadata and controls
58 lines (45 loc) · 1.07 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
package BFS;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BJ1697 {
private static boolean check[];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
check=(n<k)?new boolean[k*2+1]:new boolean[n*2+1];
bfs(n,k);
}
private static void bfs(int n,int k) {
Queue<Integer> queue=new LinkedList<Integer>();
int cnt=0;
queue.offer(n);
check[n]=true;
int temp=0,size=0;
while(!queue.isEmpty()) {
size=queue.size();
System.out.println(size);
while(size-->0) {
temp=queue.poll();
System.out.println("temp :"+temp);
if(temp==k) break;
if(temp>=1&&!check[temp-1]) {
queue.offer(temp-1);
check[temp-1]=true;
}
if(temp+1<check.length&&!check[temp+1]) {
queue.offer(temp+1);
check[temp+1]=true;
}
if(temp*2<check.length&&!check[temp*2]) {
queue.offer(temp*2);
check[temp*2]=true;
}
}
if(temp==k) break;
cnt++;
}
System.out.println(cnt);
}
}