Repository navigation
Expand file tree
/
Copy pathBOJ_1697.java
More file actions
66 lines (53 loc) · 1.74 KB
/
Copy pathBOJ_1697.java
File metadata and controls
66 lines (53 loc) · 1.74 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
66
package baekjoon.silver;
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ_1697 {
static int N, K;
static int[] road = new int[100001]; // 가능한 위치
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken()); // 수빈 위치
K = Integer.parseInt(st.nextToken()); // 동생 위치
bfs(N);
bw.flush();
bw.close();
}
public static void bfs(int start) {
if(start == K) {
System.out.println(0);
System.exit(0);
}
Queue<Integer> q = new LinkedList<>();
q.offer(start);
road[start] = 1;
while(!q.isEmpty()) {
int num = q.poll();
// 검사할 수 있는 경우의 수
for (int i = 0; i < 3; i++) {
int method;
if(i == 0) {
method = num - 1;
}
else if(i == 1) {
method = num + 1;
}
else {
method = num * 2;
}
if(method == K) {
System.out.println(road[num]);
return;
}
// 가능성 검사
if(method > 0 && method <= 100000 && road[method] == 0) {
q.offer(method);
road[method] = road[num] + 1;
}
}
}
}
}