슈콩

[BOJ] 백준 5014 스타트링크 본문

Algorithms/Baekjoon

[BOJ] 백준 5014 스타트링크

shukong 2025. 9. 30. 12:35

[문제]

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

 

 

[소스 코드]

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

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

[BOJ] 백준 2573 빙산  (0) 2025.09.30
[BOJ] 백준 2468 안전 영역  (0) 2025.09.30
[BOJ] 백준 1697 숨바꼭질  (0) 2025.09.30
[BOJ] 백준 7569 토마토  (0) 2025.09.29
[BOJ] 백준 2644 촌수계산  (0) 2025.09.29