슈콩

[BOJ] 5014 스타트링크 본문

Algorithms/Baekjoon

[BOJ] 5014 스타트링크

shukong 2026. 2. 2. 22:01

[문제]

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

 

 

[소스 코드]

import java.util.*;
import java.io.*;

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[] floors = new int[f+1];
		boolean[] visit = new boolean[f+1];
		Queue<int[]> q = new LinkedList<>();
		q.offer(new int[] {0,s});
		visit[s] = true;
		while(!q.isEmpty()) {
			int[] curr = q.poll();
			if(curr[1]==g) {
				System.out.println(curr[0]);
				return;
			}
			if(curr[1]+u<=f && !visit[curr[1]+u]) {
				visit[curr[1]+u] = true;
				q.offer(new int[] {curr[0]+1,curr[1]+u});
			}
			if(curr[1]-d>=1 && !visit[curr[1]-d]) {
				visit[curr[1]-d] = true;
				q.offer(new int[] {curr[0]+1,curr[1]-d});
			}
		}
		System.out.println("use the stairs");
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 2573 빙산  (0) 2026.02.09
[BOJ] 2468 안전 영역  (0) 2026.02.09
[BOJ] 1697 숨바꼭질  (0) 2026.01.28
[BOJ] 7569 토마토  (0) 2026.01.26
[BOJ] 2664 촌수계산  (0) 2026.01.22