Skip to content

Commit c233076

Browse files
authored
Merge pull request #358 from AlgorithmWithGod/Seol-JY
[20250615] BOJ / G4 / 숨바꼭질 2 / 설진영
2 parents 6d23e08 + 8c9f865 commit c233076

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int[] visited = new int[100001];
7+
static int minTime = Integer.MAX_VALUE;
8+
static int count = 0;
9+
static int start, target;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
start = Integer.parseInt(st.nextToken());
15+
target = Integer.parseInt(st.nextToken());
16+
17+
bfs();
18+
19+
System.out.println(minTime);
20+
System.out.println(count);
21+
}
22+
23+
public static void bfs() {
24+
Deque<Integer> queue = new ArrayDeque<>();
25+
queue.add(start);
26+
visited[start] = 1;
27+
28+
while (!queue.isEmpty()) {
29+
int current = queue.poll();
30+
int time = visited[current];
31+
32+
if (current == target) {
33+
if (time - 1 < minTime) {
34+
minTime = time - 1;
35+
count = 1;
36+
} else if (time - 1 == minTime) {
37+
count++;
38+
}
39+
continue;
40+
}
41+
42+
for (int next : new int[]{current - 1, current + 1, current * 2}) {
43+
if (next < 0 || next > 100000) continue;
44+
45+
if (visited[next] == 0 || visited[next] == time + 1) {
46+
visited[next] = time + 1;
47+
queue.add(next);
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
```

0 commit comments

Comments
 (0)