fork download
  1. #include <cstdio>
  2. #include <queue>
  3. using namespace std;
  4. int v[1000001];
  5. int main() {
  6. int f, s, g, u, d;
  7. scanf("%d%d%d%d%d", &f, &s, &g, &u, &d);
  8. if (s == g) {
  9. printf("0");
  10. return 0;
  11. }
  12. queue<int> q;
  13. q.push(s);
  14. while (!q.empty()) {
  15. int x = q.front(); q.pop();
  16. if (x + u <= f && v[x + u] == 0) {
  17. if (x + u == g) {
  18. printf("%d", v[x] + 1);
  19. return 0;
  20. }
  21. q.push(x + u);
  22. v[x + u] = v[x] + 1;
  23. }
  24. if (1 <= x - d && v[x - d] == 0) {
  25. if (x - d == g) {
  26. printf("%d", v[x] + 1);
  27. return 0;
  28. }
  29. q.push(x - d);
  30. v[x - d] = v[x] + 1;
  31. }
  32. }
  33. printf("use the stairs");
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 544KB
stdin
1000000 5 215 10 10
stdout
21