Advertisement
AdamTheGreat

Untitled

Jul 1st, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main(void) {
  7. int n, start, end;
  8. cin >> n >> start >> end;
  9. vector<bool> visited;
  10. vector<int> labels, dist;
  11. for (int i = 0; i < n; i++) {
  12. int a;
  13. cin >> a;
  14. labels.push_back(a);
  15. dist.push_back(-1);
  16. visited.push_back(false);
  17. }
  18.  
  19. // if (start == end) {
  20. // cout << 0 << endl;
  21. // }
  22.  
  23. queue<int> notebook;
  24. notebook.push(start-1);
  25. dist[start-1] = 0;
  26. visited[start-1] = true;
  27. while (!notebook.empty()) {
  28. int current = notebook.front();
  29. notebook.pop();
  30. if (dist[current] > 1000) {
  31. cout << -1 << endl;
  32. return 0;
  33. }
  34. int floor = labels[current];
  35. // cout << current << endl;
  36. if (current-floor >= 0 && current-floor < n && !visited[current-floor]) {
  37. if (current-floor == end-1) {
  38. cout << dist[current]+1 << endl;
  39. return 0;
  40. }
  41. dist[current-floor] = dist[current]+1;
  42. visited[current-floor] = true;
  43. notebook.push(current-floor);
  44. } if (current+floor >= 0 && current+floor < n && !visited[current+floor]) {
  45. if (current+floor == end-1) {
  46. cout << dist[current]+1 << endl;
  47. return 0;
  48. }
  49. dist[current+floor] = dist[current]+1;
  50. visited[current+floor] = true;
  51. notebook.push(current+floor);
  52. }
  53. // visited[current] = false;
  54. }
  55. cout << -1 << endl;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement