玄关求条
查看原帖
玄关求条
1224895
_Tatsu_楼主2024/8/20 20:50
#include <bits/stdc++.h>
#define int long long
#define fro for
using namespace std;
int n,c,y;
int dx[114514];
int a[114514]={};
struct point
{
	int pos,step;
};
int bfs (int x)
{
	queue<point>q;
	memset(a,-1,sizeof(a));
	a[x]=0;
	point qq;
	qq.pos=x,qq.step=0;
	q.push(qq);
    while (!q.empty())
    {
		point w=q.front();
		q.pop();
		int xl=w.pos+dx[w.pos],yl=w.pos-dx[w.pos];
		if (xl>=1&&xl<=n&&a[xl]==-1)
		{
			a[xl]=w.step+1;
            point tmp;
            tmp.pos=xl,tmp.step=w.step+1;
			q.push(tmp);
		}
		if (xl==y)
			return a[y];
		if (yl>=1&&yl<=n&&a[yl]==-1)
		{
			a[yl]=w.step+1;
			point tmp;
			tmp.pos=yl,tmp.step=w.step+1;
		}
		if (yl==y)
			return a[y];
    }
    return -1;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen (".in","r",stdin);
	//freopen (".out","w",stdout);
	cin>>n>>c>>y;
	for (int i=1;i<=n;i++)
	cin>>dx[i];
	cout<<bfs(c);
	return 0;
}
2024/8/20 20:50
加载中...