알고리즘 생각
문제를 보자마자 숨바꼭질문제와 똑같다는 것을 알 수 있었다.
다만 다음 좌표가 되는 정수의 범위만 다르다!!!
틀린 이유
- 범위 제한 : 숨바꼭질에서는 다음 정수가 범위 제한인 1000000을 넘어도 됐지만, 스타트링크 문제에서는 다음 정수는 이동할 층수를 의미하고, 문제에서 건물의 층수는 f로 주어지기 때문에 다음 정수는 f 이하가 되어야 한다!!!!!!
소스 구현
import java.util.*;
public class Main {
public static final int MAX = 10000000;
public static void StartLink(String[] args) {
Scanner sc = new Scanner(System.in);
int f = sc.nextInt();
int s = sc.nextInt();
int g = sc.nextInt();
int u = sc.nextInt();
int d = sc.nextInt();
int[] dist = new int[MAX];
for(int i=0;i<MAX;i++) {
dist[i] = -1;
}
Queue<Integer> q = new LinkedList<Integer>();
q.add(s);
dist[s] = 0;
while(!q.isEmpty()) {
int cur = q.remove();
for(int next : new int[] {cur-d,cur+u}) {
if(1<=next && next<=f) {
if(dist[next]==-1) {
if(next == cur-d) {
dist[next] = dist[cur]+1;
q.add(next);
} else {
dist[next] = dist[cur]+1;
q.add(next);
}
}
}
}
}
if(dist[g] != -1) {
System.out.println(dist[g]);
} else {
System.out.println("use the stairs");
}
}
}