-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1697.java
More file actions
46 lines (34 loc) · 1.09 KB
/
1697.java
File metadata and controls
46 lines (34 loc) · 1.09 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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] map = new int[200002];
Queue<Integer> q = new LinkedList<>();
q.offer(n);
while(!q.isEmpty()) {
// 현재 위치와 지금까지 걸린 시간
int now = q.poll();
int sec = map[now];
// 도착했으면 끝
if(now == k) {
System.out.println(sec);
break;
}
// 갈 수 있는 곳에 아직 방문하지 않았으면 가기
if(now+1 >= 0 && now+1 < 200002 && map[now+1] == 0) {
map[now+1] = sec + 1;
q.offer(now + 1);
}
if(now-1 >= 0 && now-1 < 200002 && map[now-1] == 0) {
map[now - 1] = sec + 1;
q.offer(now - 1);
}
if(now*2 >= 0 && now*2 < 200002 && map[now*2] == 0) {
map[now * 2] = sec + 1;
q.offer(now * 2);
}
}
}
}