Skip to content

Commit

Permalink
230722
Browse files Browse the repository at this point in the history
  • Loading branch information
NavyHubb committed Jul 22, 2023
1 parent 09353f3 commit ad2194c
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 40 deletions.
182 changes: 142 additions & 40 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions src/graph/BFS/BOJ13549.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package graph.BFS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class BOJ13549 {
static int N;
static int K;
static int[] times = new int[100_001];
static int minTime;

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

N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());

if (N >= K) {
System.out.println(N-K);
return;
}

bfs();
System.out.println(minTime-1);
}

static void bfs() {
minTime = 0;

Queue<Integer> que = new LinkedList<>();
que.add(N);
times[N] = 1;

while (!que.isEmpty()) {
int now = que.poll();

if (now == K) {
minTime = times[now];
}

int next = now*2; // 순간이동
if (next <= 100_000) {
if (times[next] == 0 || times[next] > times[now]) {
que.add(next);
times[next] = times[now];
}
}

// 앞 혹은 뒤로 한칸 이동
for (int i = 0; i < 2; i++) {
if (i == 0) next = now-1;
else next = now+1;

if (next < 0 || next > 100_000) continue;

if (times[next] == 0 || times[next] > times[now] + 1) {
que.add(next);
times[next] = times[now] + 1;
}
}

}
}

}
Loading

0 comments on commit ad2194c

Please sign in to comment.