第9个点错,求助
查看原帖
第9个点错,求助
511214
李昊邦楼主2022/1/12 16:47
#include<bits/stdc++.h>
using namespace std;
int n,a,b,k[210];
int cost[210];
queue<int>sum;
bool biao[210];
void bfs(int a,int b)
{
	sum.push(a);
	while(!sum.empty())
	{
		int c=sum.front();
		sum.pop();
		if(c==b)
		{
			cout<<cost[c];
			return;
		}
		biao[c]=true;
		int n1=c+k[c];
		if(n1>0&&n1<=n&&biao[n1]==false)
		{
			sum.push(n1);
			cost[n1]=cost[c]+1;
		}
		int n2=c-k[c];
		if(n2>0&&n2<=n&&biao[n2]==false)
		{
			sum.push(n2);
			cost[n2]=cost[c]+1;
		}
	}
	cout<<-1;
}
int main()
{
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++) cin>>k[i];
	bfs(a,b);
	return 0;
}

2022/1/12 16:47
加载中...