fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. long long int f,s,g,u,d;
  6. long long int level[10000001];
  7. bool vis[10000000];
  8. void bfs()
  9. {
  10. queue<long long int> q;
  11. q.push(s);
  12. while(!q.empty())
  13. {
  14. int curr = q.front();
  15. q.pop();
  16. //cout<<"curr level"<<curr<<" "<<level[curr]<<endl;
  17. if(curr == g)
  18. return ;
  19. if(curr + u <= f && !vis[curr +u])
  20. {
  21. level[curr + u] = 1 + level[curr];
  22. q.push(curr + u);
  23. vis[curr+u]=1;
  24. }
  25. if(curr - d >= 1 && !vis[curr-d])
  26. {
  27. level[curr - d] = 1 + level[curr];
  28. q.push(curr - d);
  29. vis[curr-d]=1;
  30. }
  31. //cout<<"hh\n";
  32. }
  33. }
  34.  
  35. int main()
  36. {
  37. cin>>f>>s>>g>>u>>d;
  38. for(int i=1;i<=f;++i)
  39. level[i] = 0,vis[i]=false;
  40. vis[s]=true;
  41. bfs();
  42. (level[g] || (s==g))? cout<<level[g] : cout<<"use the stairs";
  43. return 0;
  44. }
Success #stdin #stdout 0s 4260KB
stdin
10 10 9 0 1
stdout
1