Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22.01.13 - [BOJ] 5014. 스타트링크 #191

Closed
suhyunsim opened this issue Jan 13, 2022 · 0 comments
Closed

22.01.13 - [BOJ] 5014. 스타트링크 #191

suhyunsim opened this issue Jan 13, 2022 · 0 comments
Assignees
Labels
BFS 너비 우선 탐색 DFS 깊이 우선 탐색 골드 BOJ - 골드 성공 맞은 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jan 13, 2022

문제

핵심 아이디어

  • BFS

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q5014;

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

public class Main {
    static int f, s, g, u, d;
    static int[] visited;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        f = sc.nextInt(); //최고층
        s = sc.nextInt(); //현재 위치
        g = sc.nextInt(); //도착지
        u = sc.nextInt(); //위
        d = sc.nextInt(); //아래
        visited = new int[f + 1];
        bfs(f, s, g, u, d);
    }

    private static void bfs(int f, int s, int g, int u, int d) {
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(s);
        visited[s] = 1;
        while (!queue.isEmpty()) {
            int now = queue.poll();
            if (now == g) {
                System.out.println(visited[now] - 1);
            }
            if (now + u <= f && visited[now + u] == 0) {
                visited[now + u] = visited[now] + 1;
                queue.offer(now + u);
            }
            if (now - d > 0 && visited[now - d] == 0) {
                visited[now - d] = visited[now] + 1;
                queue.offer(now - d);
            }
        }
        if (visited[g] == 0) System.out.println("use the stairs");
    }
}
@suhyunsim suhyunsim self-assigned this Jan 13, 2022
@suhyunsim suhyunsim added BFS 너비 우선 탐색 DFS 깊이 우선 탐색 골드 BOJ - 골드 labels Jan 13, 2022
@suhyunsim suhyunsim added this to the 1월 2주 차 milestone Jan 13, 2022
@suhyunsim suhyunsim added the 성공 맞은 문제 label Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BFS 너비 우선 탐색 DFS 깊이 우선 탐색 골드 BOJ - 골드 성공 맞은 문제
Projects
None yet
Development

No branches or pull requests

1 participant