Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Seol-JY/202506/15 BOJ G4 숨바꼭질 2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
```java
import java.io.*;
import java.util.*;

public class Main {
static int[] visited = new int[100001];
static int minTime = Integer.MAX_VALUE;
static int count = 0;
static int start, target;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken());
target = Integer.parseInt(st.nextToken());

bfs();

System.out.println(minTime);
System.out.println(count);
}

public static void bfs() {
Deque<Integer> queue = new ArrayDeque<>();
queue.add(start);
visited[start] = 1;

while (!queue.isEmpty()) {
int current = queue.poll();
int time = visited[current];

if (current == target) {
if (time - 1 < minTime) {
minTime = time - 1;
count = 1;
} else if (time - 1 == minTime) {
count++;
}
continue;
}

for (int next : new int[]{current - 1, current + 1, current * 2}) {
if (next < 0 || next > 100000) continue;

if (visited[next] == 0 || visited[next] == time + 1) {
visited[next] = time + 1;
queue.add(next);
}
}
}
}
}

```