Skip to content

Commit 04283cb

Browse files
authored
Merge pull request #215 from jiminnimij/main
[1229] 13549번 - 숨바꼭질 3
2 parents e4d659a + fbb6ffc commit 04283cb

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/Problem13549.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.io.BufferedReader;
2+
import java.io.BufferedWriter;
3+
import java.io.InputStreamReader;
4+
import java.io.OutputStreamWriter;
5+
import java.io.IOException;
6+
import java.util.StringTokenizer;
7+
import java.util.Queue;
8+
import java.util.LinkedList;
9+
10+
public class Problem13549 {
11+
public static final int MAX = 100000;
12+
public static final int MIN = 0;
13+
public static boolean[] map = new boolean[MAX+1];
14+
15+
16+
public static void main(String[] args) throws IOException {
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
int n = Integer.parseInt(st.nextToken());
21+
int k = Integer.parseInt(st.nextToken());
22+
23+
Queue<int[]> q = new LinkedList<>();
24+
25+
q.add(new int[]{n, 0});
26+
27+
for (int i = 1; i < MAX+1; i++) {
28+
map[i] = false;
29+
}
30+
31+
map[n] = true;
32+
33+
while(!q.isEmpty()) {
34+
int[] temp = q.poll();
35+
int num = temp[0];
36+
int count = temp[1];
37+
38+
if (num == k) {
39+
bw.write(String.valueOf(count));
40+
bw.flush();
41+
bw.close();
42+
return;
43+
}
44+
if (num * 2 <= MAX && !map[num * 2]) {
45+
map[num * 2] = true;
46+
q.add(new int[]{num * 2, count});
47+
}
48+
if (num - 1 >= MIN && !map[num - 1]) {
49+
map[num - 1] = true;
50+
q.add(new int[]{num - 1, count + 1});
51+
}
52+
if (num + 1 <= MAX && !map[num + 1]) {
53+
map[num + 1] = true;
54+
q.add(new int[]{num + 1, count + 1});
55+
}
56+
57+
}
58+
59+
60+
}
61+
}

0 commit comments

Comments
 (0)