蜜汁20,多TLE,C++,BFS,跪求大佬看看
查看原帖
蜜汁20,多TLE,C++,BFS,跪求大佬看看
35971
加载错误楼主2018/2/25 17:02

代码:

#include<bits/stdc++.h>
using namespace std;
int e[100010],r[100010];
queue<int> q;
int main()
{
	int n,b,a,t,k,s = 0;
	cin>>n>>a>>b;
	for(int i = 1;i <= n;i++)
		cin>>e[i];
	q.push(a);
	while(!q.empty())
	{
		int x = q.front();
		if(x == b)
		{
			cout<<s<<endl;
			return 0;
		}
		q.pop();
		if(x - e[x] > 0)
			q.push(x - e[x]);
		if(x + e[x] <= n)
			q.push(x + e[x]);
		s++;
	}
	cout<<1<<endl;
	return 0;
}
2018/2/25 17:02
加载中...