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
49 changes: 49 additions & 0 deletions suyeun84/202506/17 BOJ G4 숨바꼭질 2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
```java
import java.io.*;
import java.util.*;

public class boj12851 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static int nextInt() {return Integer.parseInt(st.nextToken());}

static int N, K;
static int minTime = Integer.MAX_VALUE;
static int cnt = 0;
static int[] visited = new int[100001];
public static void main(String[] args) throws Exception {
nextLine();
N = nextInt();
K = nextInt();
bfs();
System.out.println(minTime);
System.out.println(cnt);
}

static void bfs() {
Queue<Integer> q = new LinkedList<>();
q.add(N);
visited[N] = 1;
while (!q.isEmpty()) {
int curr = q.poll();
int time = visited[curr];
if (curr == K) {
if (time-1 < minTime) {
minTime = time-1;
cnt = 1;
} else if (time-1 == minTime) cnt++;
continue;
}
for (int next : new int[] {curr-1, curr+1, curr*2}) {
if (next < 0 || next > 100000) continue;

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