64分求助
查看原帖
64分求助
1081704
lmslpLM楼主2024/1/1 21:47
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
int c[222];
int flag[222];
int sp;
int dx[] = { 1,-1 };
struct node {
	int x;
	int sum;
};
void bfs() {
	queue<node> op;
	op.push({ a,0 });
	flag[a] = 1;
	while (!op.empty()) {
		node qwq = op.front();
		op.pop();
		if (qwq.x == b) {
			cout << qwq.sum;
			sp = 1;
			return;
		}
		for (int i = 0; i < 2; i++) {
			int x = qwq.x + dx[i] * c[qwq.x];
			if (x >= 1 && x <= b && flag[x] == 0)
				op.push({ x,qwq.sum + 1 });
			flag[qwq.x] = 1;
		}
	}
}
int main()
{
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
	}
	bfs();
	if (!sp) {
		cout << "-1";
	}
	return 0;
}
```cpp
2024/1/1 21:47
加载中...