-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek2_Day5.java
More file actions
45 lines (40 loc) · 989 Bytes
/
Week2_Day5.java
File metadata and controls
45 lines (40 loc) · 989 Bytes
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
package Algorithm2;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
public class Week2_Day5 {
public static Set<Integer> visit;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int B = sc.nextInt();
visit = new HashSet<>();
int result = bfs(A,B);
System.out.println(result);
}
private static int bfs(int a, int b) {
Queue<int[]> qu =new ArrayDeque<>();
qu.offer(new int[] {a,0});
visit.add(a);
while(!qu.isEmpty()) {
int[] curr = qu.poll();
int point = curr[0];
int count = curr[1];
if(point == b) {
return count;
}
int next = 0;
int[] next_point = new int[] {point-1, point+1, point*2};
for(int i=0; i<3; i++) {
next = next_point[i];
if(next >= 0 && next <= 100000 && !visit.contains(next)) {
qu.add(new int[] {next,count+1});
visit.add(next);
}
}
}
return -1;
}
}