-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBJ_13549.java
59 lines (53 loc) · 1.83 KB
/
BJ_13549.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.*;
import java.util.*;
class Main {
static boolean[] visited;
static int[] times;
static int N, K;
static int time = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer token = new StringTokenizer(reader.readLine());
N = Integer.parseInt(token.nextToken());
K = Integer.parseInt(token.nextToken());
visited = new boolean[100001];
times = new int[100001];
Arrays.fill(times, Integer.MAX_VALUE);
if(N==K) {
System.out.println(0);
} else {
Node node = new Node(N, 0);
BFS(node);
System.out.println(times[K]);
}
}
public static void BFS(Node node) {
Queue<Node> queue = new LinkedList<>();
queue.add(node);
while(!queue.isEmpty()) {
Node now = queue.poll();
visited[now.x] = true;
//순간이동하는 경우 시간이 0초가 걸리므로 우선순위가 제일 첫 번째이다.
if(2*now.x <= 100000 && !visited[2*now.x]) {
times[2*now.x] = Math.min(times[2*now.x], now.time);
queue.add(new Node(2*now.x, now.time));
}
if(now.x+1 <= 100000 && !visited[now.x+1]) {
times[now.x+1] = Math.min(times[now.x+1], now.time+1);
queue.add(new Node(now.x+1, now.time+1));
}
if(0 <= now.x-1 && !visited[now.x-1]) {
times[now.x-1] = Math.min(times[now.x-1], now.time+1);
queue.add(new Node(now.x-1, now.time+1));
}
}
}
}
class Node {
int x;
int time;
public Node(int x, int time) {
this.x = x;
this.time = time;
}
}