-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5014.cpp
56 lines (47 loc) · 1006 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
55
56
//
// Created by 융미 on 2019-05-02.
//5014 스타트링크
#include <iostream>
#include <queue>
#include <utility>
#define MAX 1000001
using namespace std;
int f,s,g,u,d;
bool vis[MAX];
int bfs()
{
queue<pair<int,int>> q;
q.push({s,0});
vis[s] = 1;
while(!q.empty()) {
auto now = q.front();
int ncur = now.first;
int nt = now.second;
q.pop();
if (ncur == g) return nt;
if (ncur + u <= f) {
if (!vis[ncur + u]) {
vis[ncur + u] = 1;
q.push({ncur + u, nt+1});
}
}
if (ncur - d >= 1) {
if (!vis[ncur - d]) {
vis[ncur - d] = 1;
q.push({ncur - d, nt+1});
}
}
}
return -1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> f >> s >> g >> u >> d;
int result = bfs();
if(result == -1)
{
cout << "use the stairs";
}else cout << result;
return 0;
}