三个WA,四个TLE,40分
查看原帖
三个WA,四个TLE,40分
965592
Studyingson楼主2023/8/17 16:00
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
queue<int> q;
bool vis[205];
int dp[205];
int f[205];
int main()
{
	cin>>n>>a>>b;
	q.push(a);
	vis[a]=1;
	for(int i=1;i<=n;i++)
	{
		cin>>f[i];
	}
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		if(x==b) break;
		int u=x+f[x];
		int v=x-f[x];
		if(u<=n&&vis[u]==0)
		{
			dp[u]=dp[x]+1;
			q.push(u);
		}
		if(v>=1&&vis[v]==0)
		{
			dp[v]=dp[x]+1;
			q.push(v);
		}
	}
	if(a==b) cout<<"0"<<endl;
	if(dp[b]==0) cout<<"-1";
	else cout<<dp[b];
}
2023/8/17 16:00
加载中...