-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_5014.java
More file actions
70 lines (54 loc) · 1.67 KB
/
boj_5014.java
File metadata and controls
70 lines (54 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class boj_5014 {
static int F,S,G,U,D;
static int[] arr;
static boolean[] visit;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
F=Integer.parseInt(st.nextToken()); //총 개수
S=Integer.parseInt(st.nextToken()); //출발층
G=Integer.parseInt(st.nextToken()); //목표층
U=Integer.parseInt(st.nextToken()); //위로
D=Integer.parseInt(st.nextToken()); //아래로
visit=new boolean[F+1];
arr=new int[F+1];
boolean isFind=bfs(S);
if(isFind){
System.out.println(arr[G]);
}else{
System.out.println("use the stairs");
}
}
private static boolean bfs(int start){
Queue<Integer> q=new LinkedList<>();
q.add(start);
visit[start]=true;
while(!q.isEmpty()){
int cur=q.poll();
if(cur==G){
return true;
}
for(int i=0;i<2;i++){
int nxt;
if(i==0){
nxt=cur+U;
}else{
nxt=cur-D;
}
if(nxt>0 && nxt<=F && !visit[nxt]){
q.add(nxt);
visit[nxt]=true;
arr[nxt]=arr[cur]+1;
}
}
}
return false;
}
}