-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5014.cpp
54 lines (52 loc) · 945 Bytes
/
5014.cpp
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
// 5014. 스타트링크
// 2019.05.21
// BFS
#include<queue>
#include<iostream>
using namespace std;
int visit[1000001];
int dist[1000001]; // d[i] : i층을 가기 위해 눌러야 하는 버튼의 수의 최솟값
int main()
{
int F, S, G, U, D;
cin >> F >> S >> G >> U >> D;
queue<int> q;
q.push(S);
visit[S] = 1;
dist[S] = 0;
int flag = 0; // 이동할 수 있는지 없는지를 나타내는 플래그. 이동할 수 있다면 1
while (!q.empty())
{
int now = q.front();
q.pop();
if (now == G) // 목적지 도착
{
flag = 1;
break;
}
int direction[2] = { now + U, now - D };
for (int i = 0; i < 2; i++)
{
int next = direction[i];
if (1 <= next && next <= F)
{
if (visit[next])
{
continue;
}
q.push(next);
visit[next] = 1;
dist[next] = dist[now] + 1;
}
}
}
if (flag)
{
cout << dist[G] << endl;
}
else
{
cout << "use the stairs" << endl;
}
return 0;
}