쵼쥬 2022. 2. 14. 14:44


내 코드

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]);
    }
}