-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1697.java
More file actions
65 lines (54 loc) · 1.67 KB
/
1697.java
File metadata and controls
65 lines (54 loc) · 1.67 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
59
60
61
62
63
64
65
//백준 1697번
//bfs
// visit 배열을 통해 시간초 적어둠과 동시에 0이 아니면 방문함을 체크
// 무조건 K에 먼저 도착한게 빠름(큐의 선입선출 구조 때문)
import java.io.*;
import java.util.*;
public class Main {
static int N = 0;
static int K = 0;
static int cnt = Integer.MAX_VALUE;
static int[] visit;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
visit= new int[150000];
int result;
if (N==K){
result = 0;
}else{
result = bfs(N);
}
System.out.println(result);
}
public static int bfs(int x){
Queue<Integer> q = new LinkedList<>();
q.add(x);
while (!q.isEmpty()){
int n = q.peek();
q.poll();
for(int i =0;i<3;i++){
int nextPoint;
if(i == 0){
nextPoint = n - 1;
}else if (i==1){
nextPoint = n + 1;
}else {
nextPoint = 2 * n;
}
if(n==K){
return visit[n];
}
if(0<=nextPoint && nextPoint<=100000){
if(visit[nextPoint] == 0){
q.add(nextPoint);
visit[nextPoint] = visit[n] + 1;
}
}
}
}
return 0;
}
}