File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments