import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int Fsize;
static int Start;
static int Ges;
static int Up;
static int Down;
static int result;
static int arr[];
static Queue q;
static int dx[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Fsize = sc.nextInt();
Start = sc.nextInt();
Ges = sc.nextInt();
Up = sc.nextInt();
Down = sc.nextInt();
arr= new int [Fsize+1];
q = new LinkedList();
q.add(new pos(Start,0));
if(Start==Ges) {
System.out.println("0");
return;
}
BFS();
if(result==0) {
System.out.println("use the stairs");
return;
}
System.out.println(result);
}
private static void BFS() {
while(!q.isEmpty()) {
pos temp= q.poll();
int number = temp.number;
int count = temp.count;
if(number == Ges) {
result= count;
q.clear();
return;
}
for(int i=0;i<2;i++) {
int rx=0;
if(i==0) {
rx = number+Up;
}
else if(i==1) {
rx = number-Down;
}
if(rx<0||rx>Fsize||arr[rx]==1) {
continue;
}
arr[rx]=1;
q.add(new pos(rx,count+1));
}
}
}
public static class pos {
int number;
int count;
pos(int number,int count){
this.number=number;
this.count=count;
}
}
}
'코딩테스트(백준)' 카테고리의 다른 글
[백준] 2636 : 치즈 -JAVA(자바) - 사좋배 공유 (0) | 2020.02.27 |
---|---|
[백준] 1261 : 알고스팟 -JAVA - 사좋배 공유 (0) | 2020.02.27 |
[백준] 1600 : 말이 되고픈 원숭이 -JAVA(자바) - 사좋배 공유 (0) | 2020.02.24 |
[백준] 2589 : 보물섬 -JAVA(자바) - 사좋배 공유 (0) | 2020.02.23 |
[백준] 2583 : 영역 구하기 -JAVA(자바) - 사좋배 공유 (0) | 2020.02.23 |