🐣 알고리즘 삐약/💻 백준 삐약

65 삐약 : 백준 5014| 스타트 링크 [바킹독 문제 풀이|BFS|JAVA]

우주수첩 2024. 6. 4. 14:30
728x90

https://www.acmicpc.net/problem/5014

 

package BKD_0x9_BFS;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class BOJ_5014 {
    static int[] floor;
    static int[] d;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int F = sc.nextInt();
        int S = sc.nextInt();
        int G = sc.nextInt();
        int U = sc.nextInt();
        int D = sc.nextInt() * -1;

        floor = new int[F + 1];
        d = new int[]{U, D};

        System.out.println(bfs(S,G,F));


    }
    static String bfs(int start, int dst, int height){
        Queue<Integer> q = new LinkedList<>();
        q.offer(start);
        floor[start]=1;

        while (!q.isEmpty()){
            int f = q.poll();
            if(f==dst) {
                return Integer.toString(floor[f]-1);
            }
            for(int i=0;i<2;i++){
                int nd = f+d[i];

                if(nd>0 && nd<=height && floor[nd]==0){
                    floor[nd] = floor[f]+1;
                    q.offer(nd);
                }
            }
        }

        return "use the stairs";

    }
}

 

 

쉽고 빠르게 호로로록 와랄랄라라 피룰로로로롤 꺄르라라라라랄랄 냐하하하하핳하하하 풀었따.

728x90