请问这个题测试点3是什么
查看原帖
请问这个题测试点3是什么
294664
彬腾向前冲楼主2022/2/11 21:04
#include<bits/stdc++.h>

using namespace std;

const int N = 201;
int w[N];
int a,b,n;
int d[N];

void bfs() {
	queue<int> q;
	q.push(a);
	d[a] = 0;
	
	while (q.size()) {
		int t = q.front();q.pop();
		
		int d1 = t + w[t];
		if(d1 >= 1 && d1 <= n && d[d1] == 0) {
			q.push(d1);
			d[d1] = d[t] + 1;
		}
		
		int d2 = t - w[t];
		if(d2 >= 1 && d2 <= n && d[d2] == 0) {
			q.push(d2);
			d[d2] = d[t] + 1;
		}
		
		if(d1 == b || d2 == b) break;
	}
	
}

int main ()
{
	cin >> n >> a >> b;
	for(int i = 1 ; i <= n; i ++) cin >> w[i];
	
	bfs();
	
	if(a == b) return cout << 0 << endl, 0 ;
	
	if(d[n]) cout << d[n] << endl;
	else cout << -1 << endl;
	
	return 0;
}
2022/2/11 21:04
加载中...