[BOJ 1103] ์Šคํƒ€ํŠธ๋งํฌ

 

1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/1103

 

2. ํ’€์ด

+, - ๋‘˜ ๋‹ค queue์— ๋„ฃ๊ธฐ

์ง์ ‘ ๋”ํ•˜๊ฑฐ๋‚˜ ๋บ€ ํ›„ ๋น„๊ตํ•˜์ง€ ์•Š๊ณ  ๋”ํ•œ / ๋บ€ ๊ฐ’์œผ๋กœ ๋น„๊ตํ•˜๊ธฐ

 

3. ์ฝ”๋“œ

package baekjoon;

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

public class ์Šคํƒ€ํŠธ๋งํฌ {


    static int f, s, g, u, d;
    static boolean[] visited;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        f = scan.nextInt();
        s = scan.nextInt();
        g = scan.nextInt();
        u = scan.nextInt();
        d = scan.nextInt();

        visited = new boolean[f + 1];
        int result = bfs();

        if(result < 0) System.out.println("use the stairs");
        else System.out.println(result);
    }

    public static int bfs() {
        Queue<Node> q = new LinkedList<>();
        q.offer(new Node(s, 0));
        visited[s] = true;

        while(!q.isEmpty()) {
            Node current = q.poll();
            if(current.x == g) return current.cost;

            if(current.x + u <= f && visited[current.x + u] == false) {
                q.offer(new Node(current.x + u, current.cost + 1));
                visited[current.x + u] = true;
            }
            if(current.x - d >= 1 && visited[current.x - d] == false) {
                q.offer(new Node(current.x - d, current.cost + 1));
                visited[current.x - d] = true;
            }
        }
        return -1;
    }

    public static class Node {
        int x;
        int cost;

        public Node(int x, int cost) {
            this.x = x;
            this.cost = cost;
        }
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ