Skip to content

Commit c768a66

Browse files
committed
BOJ_5014 : 스타트링크
1 parent 0e12aaf commit c768a66

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

week3/BOJ_5014(스타트링크).java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int topFloor = Integer.parseInt(st.nextToken());
10+
int currentFloor = Integer.parseInt(st.nextToken());
11+
int targetFloor = Integer.parseInt(st.nextToken());
12+
int up = Integer.parseInt(st.nextToken());
13+
int down = Integer.parseInt(st.nextToken());
14+
15+
int[] visited = new int[topFloor+1];
16+
Queue<Integer> q = new LinkedList<>();
17+
q.offer(currentFloor);
18+
visited[currentFloor] = 1;
19+
while(!q.isEmpty()) {
20+
int f = q.poll();
21+
if(f == targetFloor) {
22+
System.out.println(visited[targetFloor]-1);
23+
return;
24+
}
25+
if(f+up <= topFloor && visited[f+up] == 0) {
26+
visited[f+up] = visited[f]+1;
27+
q.offer(f+up);
28+
}
29+
30+
if(f-down >= 1 && visited[f-down] == 0) {
31+
visited[f-down] = visited[f]+1;
32+
q.offer(f-down);
33+
}
34+
}
35+
System.out.println("use the stairs");
36+
}
37+
}

0 commit comments

Comments
 (0)