-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelevtrbl.cpp
51 lines (50 loc) · 1.11 KB
/
elevtrbl.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
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int main() {
int f,s,g,u,d;
scanf("%d %d %d %d %d", &f, &s, &g, &u, &d);
f--;
s--;
g--;
bool visited[1000000];
memset(visited, 0, sizeof(visited));
int pushes = 0;
queue<int> q;
q.push(s);
q.push(-1);
visited[s] = true;
bool found = false;
while(q.size() > 1 && !found) {
int front = q.front();
q.pop();
// printf("%d\n", front);
if(front == -1) {
pushes++;
q.push(-1);
continue;
}
visited[front] = true;
if(front == g) {
found = true;
break;
} else {
int up = front + u;
if(up <= f && !visited[up]) {
q.push(up);
visited[up] = true;
}
int down = front - d;
if(down >= 0 && !visited[down]) {
q.push(down);
visited[down] = true;
}
}
}
if(found) {
printf("%d\n", pushes);
} else {
printf("use the stairs\n");
}
}