반응형
5014번: 스타트링크
첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다.
www.acmicpc.net
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());
Queue<Integer> Q = new LinkedList<>();
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[] dist = new int[F+1];
Arrays.fill(dist, -1);
if (S==G){
System.out.print(0);
System.exit(0);
}
Q.offer(S);
dist[S] = 0;
while (!Q.isEmpty()){
int cur = Q.poll();
for (int dir : new int[]{U, -D}){
int n = cur + dir;
if (n<1 || n>F || dist[n]>=0) continue;
if (n==G){
System.out.print(Integer.valueOf(dist[cur]+1));
System.exit(0);
}
dist[n] = dist[cur] + 1;
Q.offer(n);
}
}
System.out.print("use the stairs");
}
}
반응형
'Algorithm' 카테고리의 다른 글
백준 6593번 상범 빌딩 [ Java ] (0) | 2021.01.02 |
---|---|
백준 2573번 빙산 [ Java ] (0) | 2021.01.02 |
백준 2468번 안전 영역 [ Java ] (0) | 2021.01.02 |
백준 10026번 적록색약 [ Java ] (0) | 2021.01.02 |
백준 7562번 나이트의 이동 [ Java ] (0) | 2021.01.02 |