是100了,但评测里有一个紫,我不李姐
查看原帖
是100了,但评测里有一个紫,我不李姐
1007656
U_stinian楼主2023/8/16 22:07
#include <bits/stdc++.h>
#define N 220
using namespace std;
int a[N], book[N];
struct data{
	int s;
	int v;
}t,x;
queue<data>q;
int main()
{
	int n, A, B, i;
	scanf("%d%d%d", &n, &A, &B);
	for(i=1; i<=n; i++)
		scanf("%d", &a[i]);
	memset(book, 0, sizeof(book));
	t.s=A;
	t.v=0;
	q.push(t);
	book[A]=1;
	while(!q.empty())
	{
		t=q.front();
		if(t.s==B)
			break;
		q.pop();
		if(t.s+a[t.s]>0 && !book[t.s+a[t.s]])
		{
			x.s=t.s+a[t.s];
			x.v=t.v+1;
			q.push(x);
			book[x.s]=1;
		}
		if(t.s-a[t.s]>=0 && !book[t.s-a[t.s]])
		{
			x.s=t.s-a[t.s];
			x.v=t.v+1;
			q.push(x);
			book[x.s]=1;
		}
	}
	if(t.s==B)
		printf("%d\n", t.v);
	else
		printf("-1\n");
	return 0;
}
2023/8/16 22:07
加载中...