广搜,70pts求调
查看原帖
广搜,70pts求调
649611
Zelensky楼主2022/10/1 11:39
using namespace std;
long long lou[1000000],q[10000000],shu[1000000],t=0,h=1;long long n,a,b,ans=1e10;
bool judge(int x){
	return x>=1;
}
void bfs(){
	q[++t]=a;//初始楼层 
	lou[a]=0;//赋零 
	while(t>=h){
		int wx=q[h];
		h++;
			if(judge(wx+shu[wx])){//楼层合法 
			q[++t]=wx+shu[wx];//压入 
			lou[wx+shu[wx]]=min(lou[wx]+1,lou[wx+shu[wx]]);//更新最小值 
		}
		if(judge(wx-shu[wx])){//同上 
			q[++t]=wx-shu[wx];
			lou[wx-shu[wx]]=min(lou[wx]+1,lou[wx-shu[wx]]);
		}
	}
}
int main()
{
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++){
		cin>>shu[i];
		lou[i]=1e10;//初始化 
	}
	bfs();
	if(lou[b]!=1e10)//判定是否访问过 
	cout<<lou[b];
	else
	cout<<-1;
	return 0;
}
2022/10/1 11:39
加载中...