救急!!64分求调!
查看原帖
救急!!64分求调!
1044521
zhz_cptbtptp楼主2024/8/18 11:09
#include<bits/stdc++.h>
using namespace std;
const int mx = 500;
int w[mx];
int n, a, b, cnt[mx], num;

void read() {
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++) {
		cin >> w[i];
	}
}

int vis[mx];

void bfs(int s) {
	queue <int> q;
	q.push(s);
	vis[s] = 1;
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		int up = x + w[x];
		int down = x - w[x];
		if (up == b || down == b) {
			cout << cnt[x]+1;
			exit(0);
		}
		if (up >= 1 && up <= n && vis[up] == 0) {
			q.push(up);
			cnt[up] = cnt[x] + 1;
			vis[up] = 1;
		}
		if (down >= 1 && down <= n && vis[down] == 0) {
			q.push(down);
			cnt[down] = cnt[x] + 1;
			vis[down] = 1;
		}
	}
}

int main() {
	read();
	bfs(1);
	cout << "-1";
}
2024/8/18 11:09
加载中...