-
Notifications
You must be signed in to change notification settings - Fork 0
/
3250.cpp
54 lines (41 loc) · 1.09 KB
/
3250.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
#include <bits/stdc++.h>
using namespace std;
int bfs(int f, int s, int g, int u, int d)
{
unordered_set<int> visited;
queue<pair<int, int>> q;
q.push({s, 0});
visited.insert(s);
while (!q.empty())
{
auto current = q.front();
q.pop();
int position = current.first;
int p_count = current.second;
if (position == g)
{
return p_count;
}
int up_position = position + u;
int down_position = position - d;
if (up_position <= f && visited.find(up_position) == visited.end())
{
q.push({up_position, p_count + 1});
visited.insert(up_position);
}
if (down_position >= 1 && visited.find(down_position) == visited.end())
{
q.push({down_position, p_count + 1});
visited.insert(down_position);
}
}
return -1;
}
int main()
{
int f, s, g, u, d;
cin >> f >> s >> g >> u >> d;
int count = bfs(f, s, g, u, d);
count == -1 ? cout << "use the stairs\n" : cout << count << endl;
return 0;
}