내 코드
package com.company;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int F = Integer.parseInt(st.nextToken());
int S = Integer.parseInt(st.nextToken());
int G = Integer.parseInt(st.nextToken());
int U = Integer.parseInt(st.nextToken());
int D = Integer.parseInt(st.nextToken());
int[] floor = new int[F + 1];
Arrays.fill(floor, -1);
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(S);
floor[S] = 0;
while (!pq.isEmpty()) {
int i = pq.poll();
if (i == G) {
break;
}
if (i + U <= F && floor[i + U] == -1) {
pq.offer(i + U);
floor[i + U] = floor[i] + 1;
}
if (i - D > 0 && floor[i - D] == -1) {
pq.offer(i - D);
floor[i - D] = floor[i] + 1;
}
}
System.out.println(floor[G] == -1 ? "use the stairs" : floor[G]);
}
}
'코딩테스트 > [백준] 코딩테스트 연습' 카테고리의 다른 글
가장 가까운 공통 조상 - 3584번 (0) | 2022.02.15 |
---|---|
모래성 - 10711번 (0) | 2022.02.14 |
호텔 - 1106번 (0) | 2022.02.10 |
문제 추천 시스템 Version 1 - 21939번 (0) | 2022.02.10 |
플로이드 2 - 11780번 (0) | 2022.02.08 |