Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- #include <cstring>
- using namespace std;
- int main() {
- while (true) {
- int N, A, B;
- cin >> N;
- if (N == 0) break;
- cin >> A >> B;
- vector<int> K(N + 1); // Floor indices from 1 to N
- for (int i = 1; i <= N; ++i) {
- cin >> K[i];
- }
- vector<int> visited(N + 1, -1); // visited[i] = number of presses to reach floor i
- queue<int> q;
- q.push(A);
- visited[A] = 0;
- while (!q.empty()) {
- int current = q.front();
- q.pop();
- if (current == B) break;
- int up = current + K[current];
- int down = current - K[current];
- if (up <= N && visited[up] == -1) {
- visited[up] = visited[current] + 1;
- q.push(up);
- }
- if (down >= 1 && visited[down] == -1) {
- visited[down] = visited[current] + 1;
- q.push(down);
- }
- }
- cout << visited[B] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement