Skip to content

Commit 277cebb

Browse files
authored
Merge pull request #365 from AlgorithmWithGod/suyeun84
[20250617] BOJ / G4 / 숨바꼭질 2 / 김수연
2 parents 3650a2d + 3685c17 commit 277cebb

File tree

1 file changed

+49
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)