-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
42 lines (32 loc) Β· 1.09 KB
/
Main.java
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
package BFS.P1697;
import java.io.*;
import java.util.*;
public class Main {
static int N, K;
static Queue<Integer> q = new LinkedList<>();
static boolean[] visited = new boolean[100001];
static int time = 0;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BFS/P1697/input.txt"));
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
q.offer(N);
loop: while (!q.isEmpty()) {
int curSize = q.size();
for (int i = 0; i < curSize; i++) {
int cur = q.poll();
visited[cur] = true;
if (cur == K) break loop;
if (isValidPath(cur+1) && !visited[cur+1]) q.offer(cur+1);
if (isValidPath(cur-1) && !visited[cur-1]) q.offer(cur-1);
if (isValidPath(cur*2) && !visited[cur*2]) q.offer(cur*2);
}
time ++;
}
System.out.println(time);
}
static boolean isValidPath(int p) {
return 0 <= p && p <= 100000;
}
}