30分求助
查看原帖
30分求助
1054257
AndyCGM楼主2023/8/23 14:00
#include <iostream>
#include <queue>
using namespace std;
int dis[210];
int vis[210];
int a,b,n;
//---
struct data{
	int floor,step;
};
//---
int isokf(int floor){
	if (floor<=0)	return 0;
	if (floor>n)	return 0;
	if (vis[floor]==1)	return 0;
	vis[floor]++;
	return 1;
}
//---
int BFS(){
	queue<data> q;
	q.push({0,0});
	while(!q.empty()){
		int nowf=q.front().floor;
		int nows=q.front().step;
		if (nowf==b)	return nows+1;
		q.pop();
		if (isokf(nowf+dis[nowf]))	q.push({nowf+dis[nowf],nows+1});
		if (isokf(nowf-dis[nowf]))	q.push({nowf-dis[nowf],nows+1});
	}
	return -1;
}
//---
int main(){
	cin >> n >> a >> b;
	for (int i=1; i<=n; i++){
		cin >> dis[i];
	}
	cout << BFS();
}
2023/8/23 14:00
加载中...